私はしばらくこれを理解しようとしてきましたが、どこにも行きませんでした。助けていただければ幸いです。
2回のフェッチでテーブルを使用しています。テーブルの最後のセクションにある 1 のみが編集可能です。アイテムを削除すると、インデックスにセクションがないと言ってアプリがクラッシュします。他の同様の質問に対するいくつかの回答を読みましたが、まだ修正できません。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[fetchedResultsController sections] count]+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section < fetchedResultsController.sections.count) {
return [[fetchedResultsController.sections objectAtIndex:section] numberOfObjects];
}
else {
return fetchedResultsControllerCustomWOD.fetchedObjects.count;
}
}
commitEditing メソッドの場合:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[[fetchedResultsControllerCustomFR objectAtIndexPath:indexPath] objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[[fetchedResultsControllerCustomFR objectAtIndexPath:indexPath] removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}