2009-06-07 43 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]; 
相關問題