Моя таблица используется для перечисления строк из основных данных.Все отлично работает, я использовал два типа данных для загрузки в одном табличном представлении.Типы данных изменяются из-за изменения кнопки области.Две кнопки области действия отображают выполненные задачи, а другие - ожидающие выполнения.Я могу искать, обновлять на столе.Хотя только заполненный список данных является редактируемым и может быть удален, иногда он работает нормально, а иногда выдает ошибку как таковую,
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;
}
Код для загрузки ячейки для просмотра таблицы выглядит следующим образом:
- (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;
}
А метод обновления, вызываемый 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];
}