0

我对 XCode 非常陌生,我正在尝试将数据填充到一个表视图中,该表视图是视图控制器的子视图。但我不断收到运行时错误,我不知道为什么。请帮忙。

我做的第一件事是在情节提要中创建视图,调整其大小,然后将其保留为动态原型表。

然后我控制单击将表格拖到我的 View Controller 类上,并且正如预期的那样,它创建了一个属性:

@interface FAViewController () <UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *myTable;

@end

请注意,我添加了 UITableViewDataSource 协议。然后我将这个类声明为数据源:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myTable.dataSource= self;
}

最后,我实现了所需的必需方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([tableView isEqual:self.myTable]) {
        return 1;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = @"Test";
    return cell;
}

当我运行该应用程序时,它会将我带到这段代码:

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([FAAppDelegate class]));
    }
}

并说信号 SIGBART。我希望我已经提供了所有相关信息。如果没有,请告诉我。

4

1 回答 1

-2

代替 :

UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

尝试:

UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}

在第一个示例中,当您说“UITableViewCell *cell = nil;”时,您是在说对象指针没有指向任何东西,然后在您尝试使用该对象时发生错误。在我给出的示例中,您正在避免这种情况发生。

于 2013-11-08T20:55:03.667 回答