2012-03-26 58 views
0

我在研究如何從didselectCellFromIndexPath方法顯示單元格上的活動指示符?來自單元格選擇的活動指標動畫

基本上我想從did選擇開始活動指標動畫,然後一旦我從解析類中得到一個返回,我將停止動畫並用勾號替換。但我不知道如何在didselectcell方法內做到這一點?這是我會使用的代碼。

cellActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
     [cell setAccessoryView:cellActivityIndicator]; 
//then 
[cellActivityIndicator startAnimating]; 
//then 
[cellActivityIndicator stopAnimating]; 

,但我只需要做好這裏面indexPath一些建議:didSelectRowAtIndexPath方法:方法

回答

2

在你didSelectRowAtIndexPath方法,您可以使用訪問細胞本身:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    //Initialise, add to cell's view and start your activity indicator 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     //Call your function or whatever work that needs to be done 
     //Code in this part is run on a background thread 

     dispatch_async(dispatch_get_main_queue(), ^(void) { 
      //Stop your activity indicator 
      //Code here is run on the main thread 
     }); 
    }); 
} 

這方法使用libdispatch/Grand Central Dispatch,並要求您安裝iOS 4或更高版本。

+0

非常感謝您的工作。 – 2012-03-27 00:03:22

+0

沒問題的朋友 – sooper 2012-03-27 00:26:24

+0

這結束了兩天的搜索和很多反覆試驗 - 謝謝! – resedasue 2012-08-07 01:05:59

2
dispatch_queue_t queue = dispatch_queue_create("Downloading image", NULL); 

dispatch_async(queue, ^{ 
    NSURL *url = [NSURL URLWithString:@"http://store.storeimages.cdn-apple.com/2441/as-images.apple.com/is/image/AppleInc/step0-edu-pricing?wid=264&hei=144&fmt=png-alpha&qlt=95"]; 

    cellActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    [cell setAccessoryView:cellActivityIndicator]; 

    NSData *downloadedImage = download data; 

    // update your UI screen 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [subViewActivityIndicator removeFromSuperview]; 
     [cell setAccessoryView:something]; 
    }); 
}); 
dispatch_release(queue);