iOS UITableView numberOfRowsInSection错误访问 [英] iOS UITableView numberOfRowsInSection BAD ACCESS

查看:203
本文介绍了iOS UITableView numberOfRowsInSection错误访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

- (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?

推荐答案

您需要在此处返回实际值。因此,无需调用 [tableView numberOfRowsInSection:[indexPath部分]] 即可,只需再次执行相同的操作即可。

You need to return an actual value here. So instead of calling [tableView numberOfRowsInSection:[indexPath section]] simply do the same thing again.

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

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:

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

这篇关于iOS UITableView numberOfRowsInSection错误访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆