2013-10-02 50 views
2

对不起,我是Cocoa的小菜鸟,因为我是iOS移动开发的新手..直接点,我用Cocoa上的GCD方法将数据分配给tableview,但是当我触发[tableview reloadData]它不工作。在这里我的示例代码:dispatch_async重新加载UITableView无法正常工作

-(void)updateCell{ 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^{ 
     NSIndexPath* indexPath; 
     // Insert code to be executed on another thread here 
     if(clean_data){ 
      for (int i=0; i<clean_data.count; i++) { 
       indexPath= [NSIndexPath indexPathForRow:i inSection:0]; 
       sqCell *cell = (sqCell *)[stockQ cellForRowAtIndexPath:indexPath]; 
       for (int j=0; j<plist.count; j++) { 
        NSString *a =[[clean_data objectAtIndex:i]objectAtIndex:1]; 
        NSString *b =[[[plist objectAtIndex:j]objectForKey:@"data"]objectAtIndex:0]; 
        if([a isEqualToString:b]){ 

         cell.last.text =[[clean_data objectAtIndex:i]objectAtIndex:1]; 
         cell.change.text = @"1231231312312"; 

        } 

       } 
      } 
     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Insert code to be executed on the main thread here 
      ///reload table here 
      [self reload]; 

     }); 
    }); 

} 

这里的方法来重新加载数据

-(void)reload{ 
    NSLog(@"reload"); 
    [stockQ setNeedsLayout]; 
    [stockQ setNeedsDisplay]; 
    [stockQ reloadData]; 

} 

控制台显示“刷新”文本,但不点火stockQ UITableCiew ..在我的代码发生什么事?

回答

0

做异步线程不做UI处理代码。修改UI(如更新的tableView) - 应始终主线程来完成。尝试从异步任务中提取代码并查看它是否有效。 另一种说法 - 为什么以任何方式需要重新加载异步任务中的数据?表视图非常轻巧&快。您的帖子中的所有代码应该在毫秒内运行。仅对长时间运行,耗时的任务使用dispatch_async!

+1

如果我没有误读OP的代码,他们_are_运行在主线程上的更新,即:'dispatch_get_main_queue()'。它不应该是一个问题,这是异步调用的方法 - 事实上,如果该方法是在主线程上运行,它可能是异步工作(如果代码已经在某些情况下更新表视图) 。 – Monolo

+0

Uhum。你有没有注意到'cell.change.text = @“1231231312312”;'是在异步线程中? – nemesis

+0

哎呀,你是对的! – Monolo

相关问题