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

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

问题描述

有什么方法可以找出 UITableView 何时从其数据源请求数据?

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

没有 viewDidLoad / viewWillAppear / viewDidAppear 方法相关的视图控制器( UITableViewController )在这里很有用,因为它们都太早了。其中没有一个(完全可以理解)保证对数据源的查询暂时完成(例如,直到视图滚动)。

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:),我需要表视图才能读取y中有足够的数据,或者 scrollRectToVisible:animated:方法调用什么都不做(如果你在<$ c中的任何一个中调用它,会发生什么? $ c> viewDidLoad , viewWillAppear viewDidAppear )。

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完成后收到通知询问数据?

This answer doesn't seem to be working anymore, due to some changes made to UITableView implementation since the answer was written. See this comment : Get notified when UITableView has finished asking for data?

我一直在玩这个问题几天,并认为继承 UITableView reloadData 是最好的方法:

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天全站免登陆