Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d49bc46398
Fetching contributors…

Cannot retrieve contributors at this time

204 lines (146 sloc) 5.647 kb
//
// SSManagedTableViewController.m
// SSDataKit
//
// Created by Sam Soffes on 4/7/12.
// Copyright (c) 2012 Sam Soffes. All rights reserved.
//
#import "SSManagedTableViewController.h"
@implementation SSManagedTableViewController
#pragma mark - Accessors
@synthesize tableView = _tableView;
@synthesize clearsSelectionOnViewWillAppear = _clearsSelectionOnViewWillAppear;
#pragma mark - NSObject
- (id)init {
return (self = [self initWithStyle:UITableViewStylePlain]);
}
- (void)dealloc {
_tableView.dataSource = nil;
_tableView.delegate = nil;
}
#pragma mark - UIViewController
- (void)loadView {
[super loadView];
// Add the table view as a subview for increased flexibility
_tableView.frame = self.view.bounds;
[self.view addSubview:_tableView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// TODO: Only reload if data is empty
[self.tableView reloadData];
if (_clearsSelectionOnViewWillAppear) {
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.tableView flashScrollIndicators];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
#pragma mark - Initializer
- (id)initWithStyle:(UITableViewStyle)style {
if ((self = [super init])) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:style];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.dataSource = self;
_tableView.delegate = self;
_clearsSelectionOnViewWillAppear = YES;
}
return self;
}
#pragma mark - Working with Cells
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Subclasses should override this method
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Subclasses should override this method
return nil;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.fetchedResultsController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
if (self.ignoreChange) {
return;
}
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
if (self.ignoreChange) {
return;
}
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 {
if (self.ignoreChange) {
return;
}
UITableView *tableView = self.tableView;
indexPath = [self viewIndexPathForFetchedIndexPath:indexPath];
newIndexPath = [self viewIndexPathForFetchedIndexPath:newIndexPath];
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 {
[super controllerDidChangeContent:controller];
if (self.ignoreChange) {
return;
}
[self.tableView endUpdates];
}
@end
Jump to Line
Something went wrong with that request. Please try again.