2009-06-07 67 views
0

我有一个UITableView与几个条目。选择一个,我需要它做一个潜在的耗时的网络操作。为了给用户一些反馈,我尝试在UITableViewCell中放置一个UIActivityIndi​​catorView。然而,微调不会出现,直到很久以后 - 我完成了昂贵的操作之后!我究竟做错了什么?UITableViewCell accessoryView不会出现,直到很晚

- (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:方法结束后才会出现。

回答

1

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

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

尝试增加[CATransaction flush]

if ([self lengthyNetworkRequest] == nil) { 
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 

} 
2

一旦你告诉ActivityIndi​​cator开始动画,你必须给你的应用程序的运行循环的机会在开始长操作之前启动动画。这可以通过将昂贵的代码移动到其自己的方法并呼叫来实现:

[self performSelector:@selector(longOperation) withObject:nil afterDelay:0]; 
相关问题