CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.

EPSReactiveCollectionViewController 1.0.1

EPSReactiveCollectionViewController 1.0.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Peter Stuart.



  • By
  • Peter Stuart

EPSReactiveCollectionViewController is a subclass of UICollectionViewController that automatically populates a collection view, and animates the insertion and deletion of items by observing changes to an array of model objects. It uses ReactiveCocoa, and is designed to be used with the MVVM pattern.

EPSReactiveTableViewController provides similar functionality for table view controllers.

Usage

Subclass EPSReactiveCollectionViewController, and write an init method which calls initWithCollectionViewLayout:bindingToKeyPath:onObject: on super to set up the binding. The value at the key path must always be an NSArray containing objects that implement -isEqual: and -hash. No object should appear in the array more than once. In the init method, register a cell class for use with the class of object that will be contained in the observed array. (The cell class must conform to <EPSReactiveCollectionViewCell>.)

- (id)init {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(60, 60);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    EPSExampleViewModel *viewModel = [EPSExampleViewModel new];

    self = [super initWithCollectionViewLayout:layout bindingToKeyPath:@"sortedObjects" onObject:viewModel];
    [self registerCellClass:[EPSColorSwatchCell class] forObjectsWithClass:[EPSColorSwatch class]];
    ...
    return self;
}

If you want to know when a cell is tapped on, subscribe to the didSelectItemSignal property.

[self.didSelectItemSignal subscribeNext:^(RACTuple *tuple) {
    RACTupleUnpack(id object, NSIndexPath *indexPath, UICollectionView *collectionView) = tuple;
    // Do something with `object`
}];

You don’t need to write any <UICollectionViewDataSource> methods.

For a more complete example of how to use EPSReactiveCollectionViewController, see the example project.

To run the example project; clone the repo, and run pod install from the Project directory first.

Requirements

EPSReactiveCollectionViewController requires ReactiveCocoa 2.2 or higher.

Installation

EPSReactiveCollectionViewController is available through CocoaPods, to install it simply add the following line to your Podfile:

pod "EPSReactiveCollectionViewController"

Alternatively, include EPSReactiveCollectionViewController.h and EPSReactiveCollectionViewController.m in your project, and install ReactiveCocoa 2.2 by following their installation instructions.

License

EPSReactiveCollectionViewController is available under the MIT license. See the LICENSE file for more info.