正在开发一个应用程序,我必须从JSON获取数据并在UITableView中显示。在后台获取数据。但它似乎无限循环。dispatch_async进入无限循环
任何帮助将不胜感激。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
queue = dispatch_queue_create("com.events.app", NULL);
dispatch_async(queue, ^{
[self load];
//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
// store the downloaded image in your model
Events *evObj = [eventsArray objectAtIndex:[indexPath row]];
NSLog(@"evObj = %@",evObj);
cell.textLabel.text = evObj.eName;
cell.detailTextLabel.text = @"Detailed Text";
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
});
});
// Configure the cell...
return cell;
}
您不能在具有返回类型的方法中使用异步方法。由于'UITableViewCell'被重用,你可以修改一个不再绑定到相同索引路径的单元。你也不应该在'tableView:cellForRowAtIndexPath:'中重新插入单元格,因为这会调用'tableView:cellForRowAtIndexPath:'。 – rckoenes 2013-04-04 12:02:38
@vitality这是不正确的,你应该在主线程上执行UI任务。当它们异步分派时,块将被添加到主队列并按顺序执行。 – rckoenes 2013-04-04 12:06:59
@rckoenes的确我不知道我的头在刚才的位置:P – 2013-04-04 12:09:13