iOS UITableView numberOfRowsInSection BAD ACCESS

时间:2012-12-17 10:41:23

标签: objective-c ios cocoa-touch uitableview

我在numberOfRowsInSection内调用UITableView委托的heightForRowAtIndexPath方法,但它给了我错误访问错误

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    NSLog(@"number of rows in section: %i", [tableView numberOfRowsInSection:[indexPath section]]);
    ...
}

有人能告诉我这里有什么不对吗?

4 个答案:

答案 0 :(得分:2)

您正在调用委托方法。

当您调用此方法时,它将调用相关的委托方法,如cellForRowAtIndexPathheightForRowAtIndexPath等。

这会导致无限循环,这就是它崩溃的原因。

答案 1 :(得分:1)

这是正常的..在那一刻,你的UITableView正在被创造。在构建之前,不应该调用与UITableView相关的方法。你应该依靠其他机制来获得你的细胞的高度。

答案 2 :(得分:1)

您需要在此处返回实际值。因此,不要再调用[tableView numberOfRowsInSection:[indexPath section]],而是再次执行相同的操作。

为了不复制您的代码,您可以创建一个帮助方法,您可以在这两个地方调用,即heightForRowAtIndexPath方法和numberOfRowsInSection方法:

- (int) myNumberOfRowsMethod{
    // Here you would usually have some underlying model 
    return [self.myContactsOrWhateverArray count];
}

答案 3 :(得分:0)

您正在致电

[tableView numberOfRowsInSection:section];

你应该打电话

[self numberOfRowsInSection:section];