2012-03-20 66 views
0

任何人都可以幫我performSelectorInBackground?我想用performSelectorInBackground中的更新數據重新載入表格。IPhone + performselector在後臺

+8

後臺沒有UI更新。 UI更改必須在主線程上執行。 – lukya 2012-03-20 09:12:31

回答

4

你可以做的只是在後臺線程中獲取數據,一旦獲取數據並更新主線程中的tableview,就可以返回主線程。

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //All your views cell creations and other stuff 
    [self performSelectorInBackground:@selector(loadDataThatToBeFetchedInThread:) 
          withObject:objectArrayThatNeedToFetchData]; 
} 

- (void) loadDataThatToBeFetchedInThread:(NSArray *)objectThatNeedToFetchData 
{ 
    //Fetch the data here. which takes place in background thread 
    [self performSelectorOnMainThread:@selector(updateTableViewWithTheData:) 
          withObject:responseData 
         waitUntilDone:YES]; 
} 


- (void) updateTableViewWithTheData:(NSMutableArray *)yourData 
{ 
    //Update Data to tableview here 
} 
+0

對不起,我只能給一個+1 :) – 2012-03-20 09:23:07

+0

^非常感謝,我一定會試一試 – Shantanu 2012-03-20 11:37:19

0

所有的UI功能都應該在主線程中完成。所以你必須在主線程中重新加載UITableView。

+0

看起來像一個很棒的評論!但也許你看看這個問題! – 2015-04-29 09:54:38