mirror of
https://github.com/zhigang1992/NTDCoreDataTableViewController.git
synced 2026-04-01 11:54:13 +08:00
Refactored some common things of a UITableViewController which is based on CoreData into this class.
95 lines
2.9 KiB
Objective-C
95 lines
2.9 KiB
Objective-C
//
|
|
// CoreDataTableViewController.m
|
|
//
|
|
//
|
|
// Created by Nicholas Tian on 3/2/14.
|
|
// Copyright (c) 2014 Nicholas Tian. All rights reserved.
|
|
//
|
|
|
|
#import "NTDCoreDataTableViewController.h"
|
|
|
|
@interface NTDCoreDataTableViewController ()
|
|
|
|
@end
|
|
|
|
@implementation NTDCoreDataTableViewController
|
|
|
|
- (NSString *)cellIdentifier
|
|
{
|
|
return nil;
|
|
}
|
|
|
|
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
|
|
}
|
|
|
|
- (NSFetchedResultsController *)newFetchedResultsController
|
|
{
|
|
return nil;
|
|
}
|
|
|
|
#pragma mark - NSFetchedResultsController Delegate
|
|
// Copied from NSFetchedResultsControllerDelegate Protocol Reference
|
|
|
|
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
|
|
[self.tableView beginUpdates];
|
|
}
|
|
|
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
|
|
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
|
|
|
|
switch(type) {
|
|
case NSFetchedResultsChangeInsert:
|
|
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
break;
|
|
|
|
case NSFetchedResultsChangeDelete:
|
|
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
|
|
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
|
|
newIndexPath:(NSIndexPath *)newIndexPath {
|
|
|
|
UITableView *tableView = self.tableView;
|
|
|
|
switch(type) {
|
|
|
|
case NSFetchedResultsChangeInsert:
|
|
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
break;
|
|
|
|
case NSFetchedResultsChangeDelete:
|
|
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
break;
|
|
|
|
case NSFetchedResultsChangeUpdate:
|
|
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
|
|
atIndexPath:indexPath];
|
|
break;
|
|
|
|
case NSFetchedResultsChangeMove:
|
|
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
|
|
[self.tableView endUpdates];
|
|
}
|
|
|
|
@end
|