2013-10-02 44 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