我一直在尝试在Xcode中实现一个简单的表视图。但是我遇到了这个错误:

[UIView tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x7fd23bc76bc0

我在网上搜索了一个答案,基本上我只能发现我正在尝试向该方法发送一些不存在的方法。如果有人可以解释这里到底发生了什么,为什么会发生,以及我将如何解决它,我将不胜感激。这是代码:

在ViewController.h中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
}

@end


在ViewController.m中:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSArray *recipes;
}

- (void)viewDidLoad {

    [super viewDidLoad];

    recipes = [NSArray arrayWithObjects:@"Eggs", @"Bacon", @"Ham", @"Steak", @"Bread", @"Lettuce", @"Tomatoes", @"Carrots", @"Milk", @"Pizza", @"Chicken", @"Soup", @"Cheese", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [recipes count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableID = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableID];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableID];
    }

    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];

    return cell;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


如果您需要更多信息,我们将很高兴为您提供信息。谢谢。

最佳答案

似乎您已将表视图的委托属性拖动到UIView而不是UiViewController上。

在情节提要中,确保将委托属性拖动到控制器上(视图顶部的黄色圆圈)。

07-24 09:51
查看更多