2011-05-11 41 views
1

我的表用於列出核心數據中的行。一切工作正常,我已經使用兩種類型的數據加載在同一個表視圖。數據類型因範圍按鈕更改而改變。兩個範圍按鈕之一顯示已完成的任務,而其他顯示未完成的任務我可以在桌面上搜索,更新。雖然只完成DATAS的列表是可編輯的,可以被刪除,有時它工作正常,有時它給誤差等,應用程序在刪除行時崩潰

Serious application error. An exception was caught from the delegate of 
NSFetchedResultsController during a call to -controllerDidChangeContent:. *** - 
[NSMutableArray insertObject:atIndex:]: index 4 beyond bounds for empty array with 
userInfo (null) 

的獲取結果控制器初始化爲,

- (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)predicate{ 

if (self.fetchedResultsControl != nil) { 
    NSLog(@"Here I am outside"); 

    return self.fetchedResultsControl; 

} 

的代碼加載小區的tableview是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell =(CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
    for (id currentObject in topLevelObjects) { 
     if([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell=(CustomCell *)currentObject; 
      break; 
     } 
    } 

} 

[self configureCell:cell atIndexPath:indexPath]; 
return cell; 

}

甲nd NSFetchedResultsControllerDelegate調用的更新方法爲;

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
[self.listingTable beginUpdates]; 
NSLog(@"Changed content"); 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id < NSFetchedResultsSectionInfo>)sectionInfo 
     atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

switch(type) { 
    case NSFetchedResultsChangeInsert: 
     [self.listingTable insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [self.listingTable deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath { 
NSLog(@"Changed content switch case"); 
UITableView *tableView = self.listingTable; 

switch(type) { 

    case NSFetchedResultsChangeInsert: 
     [self.listingTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationTop]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [self.listingTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeUpdate: 
     [self configureCell:(CustomCell *)[tableView cellForRowAtIndexPath:indexPath] 
       atIndexPath:indexPath]; 

     break; 

    case NSFetchedResultsChangeMove: 
     [self.listingTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     [self.listingTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
NSLog(@"Changed Content"); 
[self.listingTable endUpdates]; 

} 

回答

3

我沒有看到任何明顯的錯誤,但在一個tableview中換出兩個邏輯表是一個危險的設計,因爲你必須要非常小心,tableview中知道其邏輯表已經變了。

你得到的錯誤提示tableview使用一個邏輯表的索引來配置自己,但實際上是訪問第二個邏輯表的數組更短。

您的直接解決方案是仔細查看numberOfSectionsnumberOfRowsInSection等方法,以確保在切換邏輯表時能夠正確更新。

最好的設計解決方案是使用兩個獨立的tableviews和自己單獨的數據源,然後根據需要將它們交換出來。

+0

我在numberOfRowsInSection中實現了日誌,但更新是正確的,因此刪除前的行數大於1,然後刪除。 – Sandeep 2011-05-12 08:15:10

+0

但也嘗試打印託管對象的未見行,所以日誌給出的消息:(實體:Memos; id:0x7150c10 ;數據:)。但是一旦表格滾動,刪除就成爲可能。這是使用兩個單獨的列表與相同的fetchedresultscontroller和相同的表視圖的錯誤。 – Sandeep 2011-05-12 08:20:58

+0

我不能肯定你的設計是造成問題,但我認爲它可能。 – TechZen 2011-05-12 22:28:51