我想知道如何在didselectCellFromIndexPath方法的单元格上显示活动指示器?
基本上我想从did选择启动活动指示器动画然后一旦我从我的解析类返回我将停止动画并用刻度替换。但我不知道如何在didselectcell方法中做到这一点?这是我将使用的代码。
cellActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell setAccessoryView:cellActivityIndicator];
//then
[cellActivityIndicator startAnimating];
//then
[cellActivityIndicator stopAnimating];
但我只需要在indexPath中做一些建议:didSelectRowAtIndexPath:method
答案 0 :(得分: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);
答案 1 :(得分: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或更高版本。