2011-01-12 31 views
0

我有一個UITableView與3節,每節通過獨特的NSFetchedResultsController饋送。UITableView與多個NSFetchedResultsControllers導致斷言失敗

我從NSFetchedResultsController -controllerDidChangeContent,插入時,更新...表得到斷言失敗

我的猜測是indexPaths出現在下面的方法中,因爲每個控制器只有單個部分(0),而對於部分0中的控制器,失敗不會發生。

- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath 
{ 
    switch(type) 
    { 
     case NSFetchedResultsChangeInsert: 
      [[self atableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [[self atableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
      break; 
     case NSFetchedResultsChangeUpdate: 
      [self configureCell:(DashboardViewCell *) [[self atableView] cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 
     case NSFetchedResultsChangeMove: 
      [[self atableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [[self atableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

所以我的問題是,我怎麼能確定哪個控制器(從部分)正在處理,並相應修改indexPath如果是那樣的做這件事的正確方法嗎?可能有任何使用多個nsfetchedresults控制器和一個uitableview的示例。

回答

3

所以我的猜測是實際上是正確的:

每個控制器進入下面的函數

  • (無效)控制器:(NSFetchedResultsController *)控制器didChangeObject:(ID)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)類型newIndexPath:(NSIndexPath *)newIndexPath

的indexPath是不正確對每個控制器除了一個是設置爲0節

每個控制器只有一個部分 - 在我的情況下是0,但更重要的是,這些部分不對應表格部分。

所以我目前實施的(不是很大,所以很可能會返工)的解決方法是檢查控制器的cacheName,並基於該修改節indexPath/newIndexPath

是這樣的:

if([[controller cacheName] isEqualToString:@"sectionX"]) 
{ 
    indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:<add correct section>]; 
    newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:<add correct section>]; 
} 
+0

感謝您節省時間;)`newIndexPath`的一個小錯誤。它應該是`[NSIndexPath indexPathForRow:newIndexPath.row inSection:]` – 2012-12-13 08:12:14

0

我不確定這是FetchedResultController。

插入和刪除行的呼叫會在某個時候問的tableView重新加載,它會接着問numberOfRowsInSection委託和數據源的方法,等的cellForRowAtIndexPath

什麼可能發生的情況是,該模型和的tableView是不同步,並導致控制器發出警告。

[tableView beginUpdates]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     NSMutableDictionary *item = [self.itemList objectAtIndex:indexPath.row]; 
    // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.itemList removeObjectAtIndex:indexPath.row];  

} 
    [tableView endUpdates]; 

嘗試是這樣的,所有更改的tableView和底層模型被包裹在 的beginUpdates和endUpdates。這些將導致tableView等待單元格的繪製,直到你給出'OK'。

如果以上情況不是這樣的話,我通常是如何處理tableView中的多個部分的。

在你的頭文件中,你爲這些節聲明瞭一個typedef枚舉;

typedef enum { 

    SectionTypeName, 
    SectionTypeAge, 
    SectionTypeSkills, 

} SectionType; 

//in the implementation 

switch (indexPath.section) { 
    case SectionTypeName: 
     //do thing in the name section 
     break; 
    case SectionTypeAge: 
     //do thing in the name section 
     break; 
    case SectionTypeSkills: 
     //do thing in the name section 
     break;   
    default: 
     break; 
} 

我在幾乎每個tableView委託/數據源方法中都有switch()。 這使得很容易找出正在處理的部分以及它的功能。