The iOS framework that grows only as fast as its documentation
NICellFactory Class Reference

Overview

A simple factory for creating table view cells from objects.

This factory provides a single method that accepts an object and returns a UITableViewCell for use in a UITableView. A cell will only be returned if the object passed to the factory conforms to the NICellObject protocol. The created cell should ideally conform to the NICell protocol. If it does, the object will be passed to it via shouldUpdateCellWithObject: before the factory method returns.

This factory is designed to be used with NITableViewModels, though one could easily use it with other table view data source implementations simply by providing nil for the table view model.

If you instantiate an NICellFactory then you can provide explicit mappings from objects to cells. This is helpful if the effort required to implement the NICell protocol on an object outweighs the benefit of using the factory, i.e. when you want to map simple types such as NSString to cells.

Inheritance diagram for NICellFactory:
<NITableViewModelDelegate>

Tasks

(UITableViewCell *) + tableViewModel:cellForTableView:atIndexPath:withObject:
 
(void) - mapObjectClass:toCellClass:
 
(CGFloat) - tableView:heightForRowAtIndexPath:model:
 
(CGFloat) + tableView:heightForRowAtIndexPath:model:
 

Method Documentation

tableViewModel:cellForTableView:atIndexPath:withObject:

Creates a cell from a given object if and only if the object conforms to the NICellObject protocol.

+ (UITableViewCell*)tableViewModel:(NITableViewModel *)tableViewModel cellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object;
Discussion

This method signature matches the NITableViewModelDelegate method so that you can set this factory as the model's delegate:

// Must cast to id to avoid compiler warnings.
_model.delegate = (id)[NICellFactory class];

If you would like to customize the factory's output, implement the model's delegate method and call the factory method. Remember that if the factory doesn't know how to map the object to a cell it will return nil.

- (UITableViewCell *)tableViewModel:(NITableViewModel *)tableViewModel
cellForTableView:(UITableView *)tableView
atIndexPath:(NSIndexPath *)indexPath
withObject:(id)object {
UITableViewCell* cell = [NICellFactory tableViewModel:tableViewModel
cellForTableView:tableView
atIndexPath:indexPath
withObject:object];
if (nil == cell) {
// Custom cell creation here.
}
return cell;
}

Reimplemented from <NITableViewModelDelegate>.

mapObjectClass:toCellClass:

Map an object's class to a cell's class.

- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)cellClass;
Discussion

If an object implements the NICell protocol AND is found in this factory mapping, the factory mapping will take precedence. This allows you to explicitly override the mapping on a case-by-case basis.

tableView:heightForRowAtIndexPath:model:

Returns the height for a row at a given index path.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NITableViewModel *)model;
Discussion

Uses the heightForObject:atIndexPath:tableView: selector from the NICell protocol to ask the object at indexPath in the model what its height should be. If a class mapping has been made for the given object in this factory then that class mapping will be used over the result of cellClass from the NICellObject protocol.

If the cell returns a height of zero then tableView.rowHeight will be used.

Example implementation:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self.cellFactory tableView:tableView heightForRowAtIndexPath:indexPath model:self.model];
}
Parameters
tableViewThe table view within which the cell exists.
indexPathThe location of the cell in the table view.
modelThe backing model being used by the table view.
Returns
The height of the cell mapped to the object at indexPath, if it implements heightForObject:atIndexPath:tableView:; otherwise, returns tableView.rowHeight.

tableView:heightForRowAtIndexPath:model:

Returns the height for a row at a given index path.

+ (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NITableViewModel *)model;
Discussion

Uses the heightForObject:atIndexPath:tableView: selector from the NICell protocol to ask the object at indexPath in the model what its height should be. Only implicit mappings will be checked with this static implementation. If you would like to provide explicit mappings you must create an instance of NICellFactory.

If the cell returns a height of zero then tableView.rowHeight will be used.

Example implementation:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [NICellFactory tableView:tableView heightForRowAtIndexPath:indexPath model:self.model];
}
Parameters
tableViewThe table view within which the cell exists.
indexPathThe location of the cell in the table view.
modelThe backing model being used by the table view.
Returns
The height of the cell mapped to the object at indexPath, if it implements heightForObject:atIndexPath:tableView:; otherwise, returns tableView.rowHeight.