EPSReactiveTableViewController
is a subclass of UITableViewController
that automatically populates a table view, and animates the insertion and deletion of rows by observing changes to an array of model objects. It uses ReactiveCocoa, and is designed to be used with the MVVM pattern.
EPSReactiveCollectionViewController provides similar functionality for collection view controllers.
Subclass EPSReactiveTableViewController
, and write an init
method which calls initWithStyle: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 <EPSReactiveTableViewCell>
.)
- (id)init {
EPSExampleViewModel *viewModel = [EPSExampleViewModel new];
self = [super initWithStyle:UITableViewStylePlain bindingToKeyPath:@"sortedObjects" onObject:viewModel];
[self registerCellClass:[EPSNoteCell class] forObjectsWithClass:[EPSNote class]];
...
return self;
}
If you want to know when a cell is tapped on, subscribe to the didSelectRowSignal
property.
[self.didSelectRowSignal subscribeNext:^(RACTuple *tuple) {
RACTupleUnpack(id object, NSIndexPath *indexPath, UITableView *tableView) = tuple;
// Do something with `object`
}];
You don’t need to write any <UITableViewDataSource>
methods.
For a more complete example of how to use EPSReactiveTableViewController
, see the example project.
To run the example project; clone the repo, and run pod install
from the Project directory first.
EPSReactiveTableViewController requires ReactiveCocoa 2.2 or higher.
EPSReactiveTableViewController is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "EPSReactiveTableViewController"
Alternatively, include EPSReactiveTableViewController.h
and EPSReactiveTableViewController.m
in your project, and install ReactiveCocoa 2.2 by following their installation instructions.
EPSReactiveTableViewController is available under the MIT license. See the LICENSE file for more info.