2012-03-20 35 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