Create table view with blocks based syntax. This library is extracted from BaseKit.
Copy TableKit dir into your project.
Or with Cocoapods
pod 'TableKit.m', :git => "https://github.com/brunow/TableKit.m", :tag => "0.1.0"
self.tableModel = [TKListTableModel tableModelForTableView:self.tableView];
self.tableView.dataSource = self.tableModel;
self.tableView.delegate = self.tableModel;
[TKCellMapping mappingForObjectClass:[Item class] block:^(TKCellMapping *cellMapping) {
[cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];
[cellMapping onSelectRowWithBlock:^(UITableViewCell *cell, Item *item, NSIndexPath *indexPath) {
// Do something
}];
[cellMapping mapObjectToCellClass:[UITableViewCell class]];
[self.tableModel registerMapping:cellMapping];
}];
NSArray *items = [NSArray arrayWithObjects:
[Item itemWithTitle:@"Simple mapping"],
[Item itemWithTitle:@"Nib cell example"],
nil];
[self.tableModel loadTableItems:items];
TableKit provide many type of model that you can use.
Basic model that doesn't handle data list. You will need to create a block that return an object for an indexPath.
[self.tableModel setObjectForRowAtIndexPathWithBlock:^id(NSIndexPath *indexPath) {
return [self.items objectAtIndex:indexPath.row];
}];
Model that handle flat list of items without section support.
Loading items
- (void)loadTableItems:(NSArray *)items;
Model that conform to NSFetchedResultsControllerDelegate. All you need is to set a fetched result controller by using the methods below:
- (void)setFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
Or by using block syntax:
- (void)setFetchedResultsControllerWithBlock:(TKManagedTableModelFetchedResultsControllerBlock)block
Load items
- (void)loadItems
As you can see it's very simple to create a fetch controller.
After you've choosen which table model you want to use, you will need to initialize it.
self.tableModel = [TKListTableModel tableModelForTableView:self.tableView];
If you don't want to handle UITableViewDataSource and UITableViewDelegate, let TableKit handle it.
self.tableView.dataSource = self.tableModel;
self.tableView.delegate = self.tableModel;
Otherwise you will need to call table model method at hand.
[TKCellMapping mappingForObjectClass:[Item class] block:^(TKCellMapping *cellMapping) {
[cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];
[cellMapping mapObjectToCellClass:[UITableViewCell class]];
[self.tableModel registerMapping:cellMapping];
}];
[TKDynamicCellMapping mappingForObjectClass:[Item class] block:^(TKDynamicCellMapping *cellMapping) {
[cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];
[cellMapping mapObjectToCellClass:[MovieViewCell class] whenValueOfKeyPath:@"type" isEqualTo:@"movie"];
[self.tableModel registerMapping:cellMapping];
}];
Take the example above. Item object contain a property named title and MovieViewCell contain a UILabel called textLabel that herself has a property text to changed the label text.
Classes are now prefixed with TK instead of BK so just rename classes. The second change is that now you've different type of model described above.
TableKit is ARC only and require IOS 5.
- Master branch
Create sectioned model.
Bruno Wernimont
- Twitter - @brunowernimont