2011-03-29 70 views
1

我有一些與核心數據一起存儲的對象。例如,人。我將這些人與我的web服務(XML)同步,我使用NSFetchedResultsController在UITableView中顯示人員。與核心數據同步UITableViewCell

一切工作正常,我的同步方法在後臺線程中運行(sync方法只在viewDidLoad中調用一次)。

現在我想在UITableViewCell中顯示UISwitch。這個開關應該改變一個人對象的布爾值。另外,我需要顯示帶有一些圖標的小工具欄。如果人是x然後顯示y圖標,如果人是一個然後顯示b圖標...

有沒有人有任何好的想法或例子來實現這一點?

我的第一種方法: 使用方法+ UISwitch作爲子視圖在contentview中繼承和設置Core Data對象...我還需要實現layoutSubviews方法。

我不想建立一個.nib文件!我需要重複使用的電池的所有部分在其它細胞...

但我不知道這是最好的方式......

回答

0

嗨 我出了這種方式。提取結果控制器必須爲你做這個工作。 只需將u BOOL核心數據狀態連接到u uiswitch,然後製作如下:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 
    [tableView beginUpdates]; 


    switch(type) { 
     case NSFetchedResultsChangeInsert: 
     { 
      NSIndexPath *newIndex = [NSIndexPath indexPathForRow:newIndexPath.row inSection:2]; 

      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     } 
     case NSFetchedResultsChangeDelete: 
     { 
      NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:2]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     } 
     case NSFetchedResultsChangeUpdate: 
     { 
      NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:2]; 

      [self configureCell:(CompanyAndUserInfoCell *)[tableView cellForRowAtIndexPath:newIndex] atIndexPath:newIndex forTableView:tableView]; 
      break; 
     } 
     case NSFetchedResultsChangeMove: 
     { 
      NSIndexPath *newIndexStart = [NSIndexPath indexPathForRow:indexPath.row inSection:2]; 
      NSIndexPath *newIndexStop = [NSIndexPath indexPathForRow:newIndexPath.row inSection:2]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexStart] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexStop] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     } 
    } 
    [tableView endUpdates]; 


}