当 UITableView 完成请求数据时得到通知? [英] Get notified when UITableView has finished asking for data?

查看:21
本文介绍了当 UITableView 完成请求数据时得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以查明 UITableView 何时完成从其数据源请求数据?

Is there some way to find out when a UITableView has finished asking for data from its data source?

关联视图控制器 (UITableViewController) 的 viewDidLoad/viewWillAppear/viewDidAppear 方法都不是在这里使用,因为它们都过早地开火.它们中没有一个(完全可以理解)保证对数据源的查询暂时已经完成(例如,直到滚动视图).

None of the viewDidLoad/viewWillAppear/viewDidAppear methods of the associated view controller (UITableViewController) are of use here, as they all fire too early. None of them (entirely understandably) guarantee that queries to the data source have finished for the time being (eg, until the view is scrolled).

我发现的一种解决方法是在 viewDidAppear 中调用 reloadData,因为当 reloadData 返回时,表视图 保证暂时已经完成了对数据源的查询.

One workaround I have found is to call reloadData in viewDidAppear, since, when reloadData returns, the table view is guaranteed to have finished querying the data source as much as it needs to for the time being.

然而,这看起来很糟糕,因为我认为它会导致数据源在第一次被要求提供相同的信息(一次是自动的,一次是因为 reloadData 调用)已加载.

However, this seems rather nasty, as I assume it is causing the data source to be asked for the same information twice (once automatically, and once because of the reloadData call) when it is first loaded.

我想这样做的根本原因是我想保留 UITableView 的滚动位置 - 但要保留到像素级别,而不仅仅是到最近的行.

The reason I want to do this at all is that I want to preserve the scroll position of the UITableView - but right down to the pixel level, not just to the nearest row.

恢复滚动位置时(使用scrollRectToVisible:animated:),我需要表格视图中已经有足够的数据,否则scrollRectToVisible:animated:方法调用什么都不做(如果您将调用放在 viewDidLoadviewWillAppearviewDidAppear 中的任何一个中,就会发生这种情况).

When restoring the scroll position (using scrollRectToVisible:animated:), I need the table view to already have sufficient data in it, or else the scrollRectToVisible:animated: method call does nothing (which is what happens if you place the call on its own in any of viewDidLoad, viewWillAppear or viewDidAppear).

推荐答案

此答案似乎不再有效,因为自编写答案以来对 UITableView 实现进行了一些更改.请参阅此评论:UITableView 完成时收到通知要求提供数据?

我已经解决这个问题几天了,我认为子类化 UITableViewreloadData 是最好的方法:

I've been playing with this problem for a couple of days and think that subclassing UITableView's reloadData is the best approach :

- (void)reloadData {

    NSLog(@"BEGIN reloadData");

    [super reloadData];

    NSLog(@"END reloadData");

}

reloadData 在表完成重新加载其数据之前不会结束.因此,当第二个 NSLog 被触发时,表视图实际上已经完成了对数据的请求.

reloadData doesn't end before the table has finish reload its data. So, when the second NSLog is fired, the table view has actually finish asking for data.

我已经将 UITableView 子类化,以便在 reloadData 之前和之后向委托发送方法.它就像一个魅力.

I've subclassed UITableView to send methods to the delegate before and after reloadData. It works like a charm.

这篇关于当 UITableView 完成请求数据时得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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