iphone - willSelectRowAtIndexPath/didSelectRowAtIndexPath 混淆

标签 iphone uitableview spinner

这应该很容易: 调用 didSelectRowAtIndexPath 时,我运行一个复杂的方法,该方法下载 URL 的内容,对其进行解析,然后将结果放入各个属性中。 在模拟器中以及在 WiFi 环境下的设备上,一切都很好。然而,在网络速度较慢的设备上,只需要一些时间来处理这一切。 因此,我想在所选行的单元格中显示一个微调器,以使用户知道正在发生某些事情。

我的问题是,微调器似乎只在选择行的 View 与后续 View 之间的动画过渡期间出现。

我希望在下载、解析和动画过程中显示微调器。

我尝试过这个:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 myCustomCell = (MyCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
 [myCustomCell.spinner startAnimating];
 return indexPath;
 }

- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 myCustomCell = (EventCell*)[tableView cellForRowAtIndexPath:indexPath];
 [myCustomCell.spinner startAnimating];
 [self myVerySlowMethod];
 return indexPath;
 }

但是,微调器似乎仅显示在两个 View 之间。

我在真实设备上对此进行了测试,但我从未显示过微调器。

这是:

a) 因为 myVerySlowMethod 足够快? b) 因为 didSelectRowAtIndexPath 将首先处理所有内容,然后执行显示部分或 c) 在打扰你之前我应该​​先研究一下所有写得很好的编程指南?

任何答案都值得赞赏。

最佳答案

问题在于,在这两种情况下,您的 [self myVerySlowMethod] (或等效方法)都会阻塞主线程,因此 ActivityIndi​​cator 不会播放。

最好的方法是在后台线程上运行 myVerySlowMethod,然后当它完成时,它可以调用另一个方法,将新的 View Controller 推送到导航堆栈上。

首先调用后台运行的方法:

[self performSelectorInBackground:(@selector(myVerySlowMethod)) withObject:nil];

这将自动创建一个新的后台线程供您运行。然后,您必须确保在后台方法中执行以下操作:

-(void) myMethod {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   // code you want to run in background thread;
   [pool drain];
}

这是必要的,因为除了主线程之外,没有为任何线程设置默认的自动释放池。

最后,您需要将新的 View Controller 推送到主线程上的导航堆栈上。如果您有其他方法,请从后台线程调用它,如下所示:

[self performSelectorOnMainThread:(@selector(myOtherMethod)) withObject:nil
                    waitUntilDone:YES];

如果您想在之后执行其他操作,可选的第三个参数将为您保留主线程。

希望有帮助!

关于iphone - willSelectRowAtIndexPath/didSelectRowAtIndexPath 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3897821/

相关文章:

iphone - 如何将 UIView 拖入表头

java - Android Java - OnCreate 中的竞争条件,带有两个观察者并制作列表

iphone - 在 iOS 中加密和解密密码

ios - 组表单元格的透明背景

ios - DidSelectRow 方法由于 tapGestureRecognizer 而被禁用

android - 下载源代码时显示旋转轮而不是图像

android - 当警报对话框打开并且手机(模拟器)改变其方向时,应用程序崩溃

iphone - 如何在不触摸引脚的情况下触发 MKAnnotationView 的标注 View ?

ios - 如何更改 UINavigationController Title & backButton 编程?

iphone - 确定 iPad 上 UITableView 的大小