Latest 1.0.1
Homepage https://github.com/jozsef-vesza/JVRFetchedResultsControllerDataSource
License MIT
Platforms ios 5.0, requires ARC
Dependencies JVRCellConfiguratorDelegate
Frameworks CoreData
Authors

JVRFetchedResultsControllerDataSource Build Status

A reusable class that combines UITableViewDataSource and NSFetchedResultsControllerDelegate. Deletion and addition of table view cells are also supported. To use it, you will also need a class that conforms to the JVRFetchedResultsControllerDataSourceDelegate protocol, which will handle the setup and deletion of individual cells. For details about the usage, see the simple examples below, or refer to the example project.

Example for helper delegate

This object can be used to configure individual table view cells using the logic in the tableView:cellForRowAtIndexPath: method found in the UITableViewDataSource protocol. It also has access to the NSManagedObjectContext property of the data source, so it is responsible for handling item removal.

// MyHelperDelegate.h

#import "JVRFetchedResultsControllerDataSource.h"

@interface MyHelperDelegate : NSObject<JVRFetchedResultsControllerDataSourceDelegate>

@property (nonatomic, strong) NSManagedObjectContext

@end

// MyHelperDelegate.m
@implementation MyHelperDelegate

- (NSString *)fetchCellIdentifierForObject:(id)anObject
{
    if ([anObject isKindOfClass:[MyClass class]])
    {
        return @"myCell";
    }
    else
    {
        return @"regularCell";
    }
}

- (UITableViewCell *)configureCell:(MyCell *)cell withObject:(NSManagedObject *)object
{
    cell.textLabel.text = object.property;
}

- (void)deleteObject:(NSManagedObject *)object
{
    [self.managedObjectContext deleteObject:object];
    NSError *e;
    [self.managedObjectContext save:&e];
}

@end

Example usage in a Table View Controller:

With the help of JVRFetchedResultsControllerDataSource, regular table view setup and NSFetchedResultsControllerDelegate methods can be removed from UITableViewController classes, leaving room for more important parts. The data source class requires a configured NSFetchedResultsController object and a helper delegate.

// MyTableViewController.h

@implementation JVEItemsView

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupFetchedResultsDataSource];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fetchResultsControllerDataSource.paused = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.fetchResultsControllerDataSource.paused = YES;
}

- (void)setupFetchedResultsDataSource
{
    self.fetchResultsControllerDataSource = [JVRFetchedResultsControllerDataSource dataSourceForTableView:self.tableView
                                                                             withFetchedResultsController:self.fetchedResultsController
                                                                                            usingDelegate:[MyHelperDelegate delegateWithManagedObjectContext:self.managedObjectContext]];
}

@end

Installation

You can just copy the source files into your project, but the recommended way of using this class in your project is installation through CocoaPods with the following Podfile:

platform :ios, "7.1"

pod 'JVRFetchedResultsControllerDataSource'

Latest podspec

{
    "name": "JVRFetchedResultsControllerDataSource",
    "version": "1.0.1",
    "summary": "A reusable class that combines UITableViewDataSource and NSFetchedResultsControllerDelegate.",
    "homepage": "https://github.com/jozsef-vesza/JVRFetchedResultsControllerDataSource",
    "license": "MIT",
    "authors": {
        "Ju00f3zsef Vesza": "[email protected]"
    },
    "social_media_url": "http://twitter.com/j_vesza",
    "platforms": {
        "ios": "5.0"
    },
    "source": {
        "git": "https://github.com/jozsef-vesza/JVRFetchedResultsControllerDataSource.git",
        "tag": "1.0.1"
    },
    "source_files": "*.{h,m}",
    "exclude_files": "Lighter Core Data/*",
    "public_header_files": "*.h",
    "frameworks": "CoreData",
    "requires_arc": true,
    "dependencies": {
        "JVRCellConfiguratorDelegate": []
    }
}

Pin It on Pinterest

Share This