objective-c 如何在获取数据之前停止加载 UITableView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2275626/
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-03 22:56:48  来源:igfitidea点击:

How to stop UITableView from loading until data has been fetched?

iphoneobjective-c

提问by Ying

I understand that UITableViewwill call -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathmethod to get each of the cells for the table view. Say I have my data source is fetched over the internet and I have to account for latency. What will be the best way of "stopping" this method from being called? Should it block on a boolean flag in the application? Should I just call cellForRowAtIndexPathagain from within my application?

我知道这UITableView将调用-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法来获取表格视图的每个单元格。假设我的数据源是通过互联网获取的,我必须考虑延迟。“阻止”这个方法被调用的最佳方法是什么?它应该阻塞在应用程序中的布尔标志上吗?我应该cellForRowAtIndexPath从我的应用程序中再次调用吗?

I am uncertain as to when the function gets called, ie, how often the UITableView"refreshes" itself. Any explanations will be helpful! Thanks!

我不确定该函数何时被调用,即UITableView“刷新”本身的频率。任何解释都会有帮助!谢谢!

回答by mahboudz

If you don't have data, or you don't have the data for additional cells, then cellForRowAtIndex:will not be called as long as you don't tell the UTableView that you have rowCounts or new rowCounts. That value is being set in numberOfRowsInSection:.

如果您没有数据,或者您没有其他单元格的数据,那么cellForRowAtIndex:只要您不告诉 UTableView 您有 rowCounts 或 new rowCounts ,就不会被调用。该值被设置在numberOfRowsInSection:.

In other words, don't report any new cells in numberOfRowsInSection:, until you actually have that data in hand, and then cellForRowAtIndexPath:won't be called prematurely.

换句话说,不要在 中报告任何新单元格numberOfRowsInSection:,直到您真正掌握了该数据,然后cellForRowAtIndexPath:才不会过早地调用。

When you do get the additional row data, then call reloadDatato get the UITableView to ask for the number of rows and then call cellForRowAtIndex:.

当您确实获得额外的行数据时,然后调用reloadData以获取 UITableView 以询问行数,然后调用cellForRowAtIndex:.

回答by Hemang

If you've set UITableViewdatasource and delegate from IB then it will at least go for numberOfRowsInSectionmethod when you push to the view, however if you're showing data from an NSArray, it'll return count ZERO if array is still empty, so table won't go for other methods to call.

如果您已经UITableView从 IB设置了数据源和委托,那么numberOfRowsInSection当您推送到视图时,它至少会使用 for 方法,但是如果您正在显示来自 an 的数据NSArray,如果数组仍然为空,它将返回计数为零,因此表获胜不去调用其他方法。

In practice, I'm pulling data from web service to feed the table, so I am not setting up the datasource and delegate from IB instead once I get the data and status OKresponse I'd set the tableview.datasource = selfand tableview.delegate = selfand then call reloadDatamethod to update table. This ensures that it won't go for numberOfRowsInSectionmethod as you don't need to call it up without having the data.

在实践中,我是从Web服务中提取数据,而不是一旦我得到的数据和状态喂表,所以我不设立从IB的数据源和委托OK响应我设置tableview.datasource = selftableview.delegate = self,然后调用reloadData方法来更新表. 这确保它不会去寻找numberOfRowsInSection方法,因为你不需要在没有数据的情况下调用它。

回答by Anupam

When you invoke [tableView reloadData], the framework automatically invokes all the data source methods again for the table view cells that are visible currently (not for all the cells of the tableview). So the best approach would be to invoke reloadData every time you get data from the internet.

当您调用 时[tableView reloadData],框架会自动为当前可见的表格视图单元格再次调用所有数据源方法(不是为表格视图的所有单元格)。因此,最好的方法是每次从 Internet 获取数据时调用 reloadData。

回答by Greg Martin

What you should do is call reloadData on your table view when your data is done being fetched. That will force the table view to call it's delegate methods again for display. Those methods are called whenever the table view needs to re-display, so either when it comes into view, scrolling occurs, or you manually call reloadData.

您应该做的是在完成获取数据后在表视图上调用 reloadData 。这将强制表视图再次调用它的委托方法进行显示。每当表视图需要重新显示时都会调用这些方法,因此当它进入视图时会发生滚动,或者您手动调用 reloadData。