Created
July 10, 2014 15:19
-
-
Save g-1/904495e30191c83dcd50 to your computer and use it in GitHub Desktop.
UITableViewController+CoreDataでセクション内セルを削除を行う場合 ref: http://qiita.com/g-1/items/549402748573adf46acc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
//... | |
if (editingStyle == UITableViewCellEditingStyleDelete) { | |
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
NSArray* sections = [self.fetchedResultsController sections]; | |
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section]; | |
return [sectionInfo numberOfObjects]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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