mirror of
https://github.com/zhigang1992/NTDCoreDataTableViewController.git
synced 2026-01-13 08:20:28 +08:00
138 lines
4.2 KiB
Objective-C
138 lines
4.2 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
|
|
|
|
#pragma mark - Incomplete Method Implementation
|
|
|
|
|
|
- (NSString *)cellIdentifier
|
|
{
|
|
return nil;
|
|
}
|
|
|
|
- (NSFetchedResultsController *)newFetchedResultsController
|
|
{
|
|
return nil;
|
|
}
|
|
|
|
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
|
|
}
|
|
|
|
#pragma mark - Table View Data Source
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
NSParameterAssert(indexPath.row < [self tableView:self.tableView numberOfRowsInSection:indexPath.section]);
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self cellIdentifier] forIndexPath:indexPath];
|
|
|
|
// Configure the cell...
|
|
[self configureCell:cell atIndexPath:indexPath];
|
|
|
|
return cell;
|
|
}
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
{
|
|
return [[self.fetchedResultsController sections] count];
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
{
|
|
NSInteger numberOfRows = 0;
|
|
|
|
if ([[self.fetchedResultsController sections] count] > 0) {
|
|
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
|
|
numberOfRows = [sectionInfo numberOfObjects];
|
|
}
|
|
|
|
return numberOfRows;
|
|
}
|
|
|
|
#pragma mark - NSFetchedResultsController and Delegate
|
|
|
|
- (NSFetchedResultsController *)fetchedResultsController
|
|
{
|
|
if (_fetchedResultsController == nil) {
|
|
_fetchedResultsController = [self newFetchedResultsController];
|
|
}
|
|
|
|
return _fetchedResultsController;
|
|
}
|
|
|
|
// 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
|