简体   繁体   English

UITableView numberOfRowsInSection 被多次调用

[英]UITableView numberOfRowsInSection called multiple times

How many times should the tableView:numberOfRowsInSection method be called when populating a table view?填充表格视图时应该调用tableView:numberOfRowsInSection方法多少次?

My application is crashing with no warnings, errors, or a stack trace.我的应用程序崩溃,没有警告、错误或堆栈跟踪。 I have also tested for memory leaks and have found none.我还测试了内存泄漏,但没有发现。 The app holds steady at about 1.4MB.该应用程序保持稳定在 1.4MB 左右。

I have NSLog reporting as methods are messaged, and I notice that tableView:numberOfRowsInSection is being called multiple times.当方法被发送消息时,我有 NSLog 报告,我注意到tableView:numberOfRowsInSection被多次调用。 The app crashes during one of these "extra" calls.该应用程序在这些“额外”调用之一期间崩溃。 The point at which the app crashes varies.应用程序崩溃的点各不相同。 I should point out that the table is populating about 600 cells, if that makes a difference.我应该指出,如果这有所不同的话,该表将填充大约 600 个单元格。

I can post code if you want, but my data source comes from a singleton class , so It is a lot of code.如果你愿意,我可以发布代码,但我的数据源来自一个singleton class ,所以代码很多。 Any help would be greatly appreciated.任何帮助将不胜感激。

I faced the same problem, the first time tableview instance is not nil and the subsequent times it is nil.我遇到了同样的问题,第一次 tableview 实例不是 nil,随后它是 nil。 The problem was, I had yet not set the return value for问题是,我还没有设置返回值

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

and because of this it kept happening.正因为如此,它一直在发生。 Once I set a return value the problem was gone.一旦我设置了返回值,问题就消失了。 Hope this helps someone.希望这可以帮助某人。

See my answer in another similar question: link请参阅我在另一个类似问题中的回答: 链接
Basically, calls to the the delegate methods of a table view can be triggered by different events.基本上,对表视图的委托方法的调用可以由不同的事件触发。 It wouldn't be a suprise to me that UITableView calls numberOfRowsInSection multiple times (even for the same section). UITableView 多次调用numberOfRowsInSection对我来说并不奇怪(即使对于同一部分)。

the best way is to use Lazy table image loading .最好的方法是使用Lazy table 图像加载 so that the 600 cells is a big amount and if you call all the data in a single time it will always going to be crashed due to memory issues.所以 600 个单元是一个很大的数量,如果你一次调用所有数据,它总是会因为内存问题而崩溃。

try using the code I provided .尝试使用我提供的代码。

Another answer on the stackoverflow can help you out. stackoverflow上的另一个答案可以帮助您。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM