xcode 多次调用 UITableView numberOfRowsInSection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5230555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:24:42  来源:igfitidea点击:

UITableView numberOfRowsInSection called multiple times

iphoneobjective-cxcodeuitableview

提问by Chris

How many times should the tableView:numberOfRowsInSectionmethod 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:numberOfRowsInSectionis being called multiple times. 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.

当方法被发送消息时,我有 NSLog 报告,我注意到它tableView:numberOfRowsInSection被多次调用。该应用程序在这些“额外”调用之一期间崩溃。应用程序崩溃的点各不相同。我应该指出,如果这有所不同的话,该表将填充大约 600 个单元格。

I can post code if you want, but my data source comes from a singleton class, so It is a lot of code. Any help would be greatly appreciated.

如果你愿意,我可以发布代码,但我的数据源来自singleton class,所以代码很多。任何帮助将不胜感激。

回答by Andrei Stanescu

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 numberOfRowsInSectionmultiple times (even for the same section).

请参阅我在另一个类似问题中的回答:link
基本上,对表视图的委托方法的调用可以由不同的事件触发。UITableViewnumberOfRowsInSection多次调用(即使是同一部分)对我来说并不奇怪。

回答by Omar

I faced the same problem, the first time tableview instance is not nil and the subsequent times it is nil. The problem was, I had yet not set the return value for

我遇到了同样的问题,第一次 tableview 实例不是 nil,随后它是 nil。问题是,我还没有设置返回值

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

正因为如此,它一直在发生。一旦我设置了返回值,问题就消失了。希望这可以帮助某人。

回答by V.V

the best way is to use Lazy table image loading. 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.

最好的方法是使用Lazy table 图像加载。所以 600 个单元是一个很大的数量,如果你一次调用所有数据,它总是会因为内存问题而崩溃。

try using the code I provided .

尝试使用我提供的代码。

Another answer on the stackoverflow can help you out.

stackoverflow上的另一个答案 可以帮助您。