2012-04-23 48 views
0

我有一個tableviewcontroller其tableviewcells直接原因請看另一個tableviewcontroller。 segue是從tableViewCell原型創建到故事板中的「子」tableview的。我在父級tableview中使用prepareForSegue,如下所示。當如下線程,這被在主線程把「背」(因爲它是UI相關的)的代碼被執行後的新孩子的tableview的方法執行(例如的cellForRowAtIndexPath,numberOfRowsInSection,viewDidLoad中)。結果是子表tableView沒有建立(例如numberRows = 0)。跨越SEGUE序列化線程的tableView

我由新的視圖的設置器(setPhotoList,setPhotoListTitle)內執行taleView reloadData「固定」這一點。是否有一種更好的方法來代替同步/序列化一個segue的主線程?我的修復看起來有點低效,因爲子表table被繪製,但是如果兩個setter(在這種情況下)重新加載,則必須重新加載一次甚至兩次。謝謝。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

UITableViewCell *cell = sender; 
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; 

dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NULL); 
dispatch_async(downloadQueue, ^{ 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];   
    NSDictionary *place = [self.places objectAtIndex:indexPath.row]; 
    NSArray *photosHere = [FlickrFetcher photosInPlace:place maxResults:MAX_NUM_PHOTOS]; 
    NSString *placeAll = [place objectForKey:FLICKR_PLACE_NAME]; 
    NSArray *placeComponents = [placeAll componentsSeparatedByString:@","]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 

     if ([segue.identifier isEqualToString:@"Show Photos At Place"]) { 
      [segue.destinationViewController setPhotoList:photosHere]; 
      [segue.destinationViewController setPhotoListTitle:[placeComponents objectAtIndex:0]]; 
     }   

    }); 
}); 
dispatch_release(downloadQueue); 

}

回答

0

......怎麼

    兒童視圖控制器的制定者
  1. ,設置一個標誌。它指示子視圖控制器是否應該重新加載數據。
  2. 在兒童視圖控制器viewWillAppearviewDidAppear,重新加載數據使用downloadQueue,這取決於該標誌。
+0

t_motooka ...關於你的建議: – 2012-04-23 19:45:34

+0

t_motooka ...謝謝,我認爲你的建議在這裏消除了我在setter中這樣做的雙重reloadData。但不知道這是我正在尋找,因爲我想阻止初始視圖加載,直到prepareForSegue完成。 – 2012-04-23 21:11:03