Skip to content

Instantly share code, notes, and snippets.

@g-1
Created July 10, 2014 15:19
Show Gist options
  • Save g-1/904495e30191c83dcd50 to your computer and use it in GitHub Desktop.
Save g-1/904495e30191c83dcd50 to your computer and use it in GitHub Desktop.
UITableViewController+CoreDataでセクション内セルを削除を行う場合 ref: http://qiita.com/g-1/items/549402748573adf46acc
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//CoreDataから削除
NSInteger numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
self.isSectionDeleted = (numRows == 1);
NSManagedObject* managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
[[MapData sharedManager].managedObjectContext deleteObject:managedObject];
[[MapData sharedManager] save];
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView* tableView = self.tableView;
switch (type) {
case NSFetchedResultsChangeDelete:
NSLog(@"indexPath.row=%ld section=%ld", indexPath.row, indexPath.section);
if (self.isSectionDeleted) {
NSLog(@"isSectionDeleted");
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
break;
default:
break;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray* sections = [self.fetchedResultsController sections];
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
NSInteger numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
self.isSectionDeleted = (numRows == 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment