简体   繁体   English

iOS UITableView numberOfRowsInSection错误访问

[英]iOS UITableView numberOfRowsInSection BAD ACCESS

I'm calling the numberOfRowsInSection method of the UITableView delegate inside of the heightForRowAtIndexPath but it gives me a Bad Access Error : 我在heightForRowAtIndexPath内部调用UITableView委托的numberOfRowsInSection方法,但它给我带来了heightForRowAtIndexPath 访问错误

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

Can anybody tell me whats wrong here? 有人可以告诉我这是怎么回事吗?

You are calling an delegate method. 您正在调用委托方法。

When you call this method, it'll invoke the related delegates method like cellForRowAtIndexPath , heightForRowAtIndexPath etc. 调用此方法时,它将调用相关的委托方法,例如cellForRowAtIndexPathheightForRowAtIndexPath等。

It'll cause an infinite loop that's why it's crashing. 这将导致无限循环 ,这就是崩溃的原因。

That's normal.. In that moment your UITableView is being created. 那是正常的。在那一刻,您的UITableView被创建。 You shouldn't call methods related to your UITableView , before it has been build. 在构建UITableView之前,不应调用与之相关的方法。 You should rely in other mechanism to get the height of your Cells. 您应该依靠其他机制来获取单元格的高度。

You need to return an actual value here. 您需要在此处返回实际值。 So instead of calling [tableView numberOfRowsInSection:[indexPath section]] simply do the same thing again. 因此,无需再次调用[tableView numberOfRowsInSection:[indexPath section]]只需再次执行相同的操作即可。

In order not to duplicate your code you could create a helper method which you can call in both places, namely, your heightForRowAtIndexPath method and the numberOfRowsInSection method: 为了不重复代码,您可以创建一个可以在两个地方调用的助手方法,即heightForRowAtIndexPath方法和numberOfRowsInSection方法:

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

You are calling 你在打电话

[tableView numberOfRowsInSection:section];

You should be calling 你应该打电话

[self numberOfRowsInSection:section];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS UITableView numberOfRowsInSection在tableView heightForRowAtIndexPath中给予不良访问 - iOS UITableView numberOfRowsInSection in tableView heightForRowAtIndexPath give BAD ACCESS UITableView numberOfRowsInSection - UITableView numberOfRowsInSection 用户交互时,iOS UITableView获取EXC_BAD_ACCESS - iOS UITableView gets EXC_BAD_ACCESS when user interracts UITableView'reloadSections:'方法在iOS 9中使用EXEC_BAD_ACCESS崩溃 - UITableView 'reloadSections:' method crash in iOS 9 with EXEC_BAD_ACCESS EXC_BAD_ACCESS:核心数据,NSFetchedResultsController和numberOfRowsInSection - EXC_BAD_ACCESS: Core Data, NSFetchedResultsController and numberOfRowsInSection 表格视图中的“ EXEC_BAD_ACCESS”,“ [[TasksPageViewController tableView:numberOfRowsInSection:]” - 'EXEC_BAD_ACCESS' in Table View, “[TasksPageViewController tableView:numberOfRowsInSection:]” uitableview numberOfRowsInSection计数 - uitableview numberOfRowsInSection count 没有为UITableView调用numberOfRowsInSection - numberOfRowsInSection not being called for UITableView UITableView numberOfRowsInSection在IOS8上工作正常,但在IOS7上有奇怪的行为 - UITableView numberOfRowsInSection works fine on IOS8 but has weird behaviour on IOS7 什么时候是tableView:numberOfRowsInSection:在UITableView中调用? - When is tableView:numberOfRowsInSection: called in UITableView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM