UITableViewCell accessoryView直到很久以后才出现

时间:2009-06-07 14:43:36

标签: iphone objective-c cocoa-touch

我有一个带有几个条目的UITableView。选择一个后,我需要它来进行潜在的耗时网络操作。为了给用户一些反馈,我尝试在UITableViewCell中放置一个UIActivityIndi​​catorView。然而,旋转器直到很久以后才出现 - 在我完成了昂贵的操作之后!我做错了什么?

- (NSIndexPath *) tableView:(UITableView *) tableView
   willSelectRowAtIndexPath:(NSIndexPath *) indexPath {

  UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
                                       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

  [spinner autorelease];
  [spinner startAnimating];
  [[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];

  if ([self lengthyNetworkRequest] == nil) {
    // ...

    return nil;
  }

  return indexPath;
}

如您所见,我在漫长的网络操作之前​​将微调器设置为accessoryView。但它仅在tableView:willSelectRowAtIndexPath:方法完成后才会显示。

3 个答案:

答案 0 :(得分:2)

一旦告诉ActivityIndi​​cator开始设置动画,就必须在启动长操作之前为应用程序的运行循环提供启动动画的机会。这可以通过将昂贵的代码移动到自己的方法并调用:

来实现
[self performSelector:@selector(longOperation) withObject:nil afterDelay:0];

答案 1 :(得分:1)

编辑:我认为你应该使用didSelect而不是willSelect。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

尝试在

之前添加[CATransaction flush]
if ([self lengthyNetworkRequest] == nil) {

答案 2 :(得分:0)

- (NSIndexPath *) tableView:(UITableView *) tableView
   willSelectRowAtIndexPath:(NSIndexPath *) indexPath {

  UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
                                       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

  [spinner autorelease];
  [spinner startAnimating];
  [[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];

  if ([self lengthyNetworkRequest] == nil) {

    //doing the intensive work after a delay so the UI gets updated first

    [self performSelector:@selector(methodThatTakesALongTime) withObject:nil afterDelay:0.25];

    //you could also choose "performSelectorInBackground"


  }

  return indexPath;
}


- (void)methodthatTakesALongTime{

    //do intensive work here, pass in indexpath if needed to update the spinner again

}
相关问题