Skip to content

Instantly share code, notes, and snippets.

@mbbischoff
Last active August 29, 2015 14:15
Show Gist options
  • Save mbbischoff/70948b8f39bd5e8b4ed3 to your computer and use it in GitHub Desktop.
Save mbbischoff/70948b8f39bd5e8b4ed3 to your computer and use it in GitHub Desktop.
FetchedResultsControllerTableViewDataSource. Please stop writing this over and over again.
//
// FetchedResultsControllerTableViewDataSource.h
// Flock
//
// Created by Matthew Bischoff on 2/14/15.
// Copyright (c) 2015 Lickability. All rights reserved.
//
@import Foundation;
@import CoreData;
@import UIKit;
typedef BOOL(^TableViewDataSourceBooleanBlock)(UITableView *tableView, NSIndexPath *indexPath);
typedef NSString *(^TableViewDataSourceSectionFooterBlock)(UITableView *tableView, NSInteger section);
typedef void(^TableViewDataSourceEditBlock)(UITableView *tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath *indexPath);
typedef UITableViewCell *(^TableViewDataSourceCellBlock)(UITableView *tableView, NSIndexPath *indexPath, id object);
@interface FetchedResultsControllerTableViewDataSource : NSObject <UITableViewDataSource>
- (instancetype)initWithFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
cellBlock:(TableViewDataSourceCellBlock)cellBlock NS_DESIGNATED_INITIALIZER;
@property (nonatomic, weak) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic) BOOL showsSectionTitles;
@property (nonatomic, copy) TableViewDataSourceBooleanBlock canEditRowBlock;
@property (nonatomic, copy) TableViewDataSourceBooleanBlock canMoveRowBlock;
@property (nonatomic, copy) TableViewDataSourceSectionFooterBlock footerTitleBlock;
@property (nonatomic, copy) TableViewDataSourceEditBlock commitEditingStyleBlock;
@end
//
// FetchedResultsControllerTableViewDataSource.m
// Flock
//
// Created by Matthew Bischoff on 2/14/15.
// Copyright (c) 2015 Lickability. All rights reserved.
//
#import "FetchedResultsControllerTableViewDataSource.h"
@interface FetchedResultsControllerTableViewDataSource ()
@property (nonatomic, copy) UITableViewCell * (^cellBlock)(UITableView *tableView, NSIndexPath *indexPath, id object);
@end
@implementation FetchedResultsControllerTableViewDataSource
- (instancetype)initWithFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController cellBlock:(TableViewDataSourceCellBlock)cellBlock {
self = [super init];
NSParameterAssert(fetchedResultsController);
NSParameterAssert(cellBlock);
if (self) {
_fetchedResultsController = fetchedResultsController;
_cellBlock = [cellBlock copy];
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.fetchedResultsController.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return sectionInfo.numberOfObjects;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (self.cellBlock) {
return self.cellBlock(tableView, indexPath, object);
}
return nil;
}
- (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];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return sectionInfo.name;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.canEditRowBlock) {
return self.canEditRowBlock(tableView, indexPath);
}
return NO;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (self.footerTitleBlock) {
return self.footerTitleBlock(tableView, section);
}
return nil;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.canMoveRowBlock) {
self.canMoveRowBlock(tableView, indexPath);
}
return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.commitEditingStyleBlock) {
self.commitEditingStyleBlock(tableView, editingStyle, indexPath);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment