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

Overview

A simple factory for creating collection view cells from objects.

This factory provides a single method that accepts an object and returns a UICollectionViewCell for use in a UICollectionView. A cell will only be returned if the object passed to the factory conforms to the NICollectionViewCellObject protocol. The created cell should ideally conform to the NICollectionViewCell 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 NICollectionViewModel, though one could easily use it with other collection view data source implementations simply by providing nil for the collection view model argument.

If you instantiate an NICollectionViewCellFactory then you can provide explicit mappings from objects to cells. This is helpful if the effort required to implement the NICollectionViewCell 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 NICollectionViewCellFactory:
<NICollectionViewModelDelegate>

Tasks

(UICollectionViewCell *) + collectionViewModel:cellForCollectionView:atIndexPath:withObject:
 
(void) - mapObjectClass:toCellClass:
 
(Class) - collectionViewCellClassForItemAtIndexPath:model:
 
(Class) + collectionViewCellClassForItemAtIndexPath:model:
 
(UICollectionReusableView *) - collectionViewModel:collectionView:viewForSupplementaryElementOfKind:atIndexPath:
 

Method Documentation

collectionViewModel:cellForCollectionView:atIndexPath:withObject:

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

+ (UICollectionViewCell*)collectionViewModel:(NICollectionViewModel *)collectionViewModel cellForCollectionView:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object;
Discussion

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

// Must cast to id to avoid compiler warnings.
_model.delegate = (id)[NICollectionViewCellFactory 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.

- (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel
cellForCollectionView:(UICollectionView *)collectionView
atIndexPath:(NSIndexPath *)indexPath
withObject:(id)object {
UICollectionViewCell* cell = [NICollectionViewCellFactory collectionViewModel:collectionViewModel
cellForCollectionView:collectionView
atIndexPath:indexPath
withObject:object];
if (nil == cell) {
// Custom cell creation here.
}
return cell;
}

Reimplemented from <NICollectionViewModelDelegate>.

mapObjectClass:toCellClass:

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

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

If an object implements the NICollectionViewCell 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.

collectionViewCellClassForItemAtIndexPath:model:

Returns the mapped cell class for an object at a given index path.

- (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
Discussion

Explicitly mapped classes in the receiver take precedence over implicitly mapped classes.

This method is helpful when implementing layout calculation methods for your collection view. You can fetch the cell class and then perform any selectors that are necessary for calculating the dimensions of the cell before it is instantiated.

collectionViewCellClassForItemAtIndexPath:model:

Returns the mapped cell class for an object at a given index path.

+ (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
Discussion

This method is helpful when implementing layout calculation methods for your collection view. You can fetch the cell class and then perform any selectors that are necessary for calculating the dimensions of the cell before it is instantiated.

collectionViewModel:collectionView:viewForSupplementaryElementOfKind:atIndexPath:

Fetches a supplementary collection view element at a given indexPath.

- (UICollectionReusableView*)collectionViewModel:(NICollectionViewModel *)collectionViewModel collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
Discussion

The value of the kind property and indexPath are implementation-dependent based on the type of UICollectionViewLayout being used.