Skip to content

Commit

Permalink
[collections] Provide access to the cell factory classes rather than …
Browse files Browse the repository at this point in the history
…embed size calculations.

Different layouts calculate sizes in different ways so there's no effective
way to define generic layout calculation for collection items.
  • Loading branch information
jverkoey committed Mar 28, 2013
1 parent 297bf27 commit 4ef57bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 111 deletions.
88 changes: 2 additions & 86 deletions src/collections/src/NICollectionViewCellFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,65 +85,9 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
*/
- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)collectionViewCellClass;

/**
* Returns the size for a view at a given index path.
*
* Uses the sizeForObject:atIndexPath:collectionView: selector from the NICollectionViewCell
* protocol to ask the object at indexPath in the model what its size 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 NICollectionViewCellObject protocol.
*
* If the cell returns a size of zero then collectionViewLayout.itemSize will be used.
*
* Example implementation:
*
@code
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [self.cellFactory collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath model:self.model];
}
@endcode
*
* @param collectionView The collection view within which the item exists.
* @param layout The layout of the collection view.
* @param indexPath The location of the cell in the collection view.
* @param model The backing model being used by the collection view.
* @returns The size of the cell mapped to the object at indexPath, if it implements
* sizeForObject:atIndexPath:tableView:; otherwise, returns
* collectionViewLayout.itemSize.
*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
- (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;

/**
* Returns the size for a view at a given index path.
*
* Uses the sizeForObject:atIndexPath:collectionView: selector from the NICollectionViewCell
* protocol to ask the object at indexPath in the model what its size 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 NICollectionViewCellObject protocol.
*
* If the cell returns a size of zero then collectionViewLayout.itemSize will be used.
*
* Example implementation:
*
@code
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [NICollectionViewCellFactory collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath model:self.model];
}
@endcode
*
* @param collectionView The collection view within which the item exists.
* @param layout The layout of the collection view.
* @param indexPath The location of the cell in the collection view.
* @param model The backing model being used by the collection view.
* @returns The size of the cell mapped to the object at indexPath, if it implements
* sizeForObject:atIndexPath:tableView:; otherwise, returns
* collectionViewLayout.itemSize.
*/
+ (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
+ (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;

@end

Expand Down Expand Up @@ -195,34 +139,6 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
*/
- (BOOL)shouldUpdateCellWithObject:(id)object;

@optional

/**
* Asks the receiver to calculate its size.
*
* The following is an appropiate implementation in your collectionView's delegate:
*
@code
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat size = collectionViewLayout.itemSize;
id object = [(NICollectionViewModel *)collectionView.dataSource objectAtIndexPath:indexPath];
id class = [object collectionViewCellClass];
if ([class respondsToSelector:@selector(sizeForObject:atIndexPath:collectionView:)]) {
size = [class sizeForObject:object atIndexPath:indexPath collectionView:collectionView];
}
return size;
}
@endcode
*
* You may also use the
* @link NICollectionViewCellFactory::collectionView:sizeForRowAtIndexPath:model: collectionView:sizeForRowAtIndexPath:model:@endlink
* methods on NICollectionViewCellFactory to achieve the same result. Using the above example allows you to
* customize the logic according to your specific needs.
*/
+ (CGSize)sizeForObject:(id)object atIndexPath:(NSIndexPath *)indexPath collectionView:(UICollectionView *)collectionView;

@end

/**
Expand Down
29 changes: 4 additions & 25 deletions src/collections/src/NICollectionViewCellFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,20 @@ - (void)mapObjectClass:(Class)objectClass toCellClass:(Class)collectionViewCellC


///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
CGSize size = CGSizeZero;
if ([collectionViewLayout respondsToSelector:@selector(itemSize)]) {
size = [(id)collectionViewLayout itemSize];
}
- (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
id object = [model objectAtIndexPath:indexPath];
Class collectionViewCellClass = [self collectionViewCellClassFromObject:object];
if ([collectionViewCellClass respondsToSelector:@selector(sizeForObject:atIndexPath:collectionView:)]) {
CGSize cellSize = [collectionViewCellClass sizeForObject:object atIndexPath:indexPath collectionView:collectionView];
if (cellSize.width > 0 && cellSize.height > 0) {
size = cellSize;
}
}
return size;
return [self collectionViewCellClassFromObject:object];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
CGSize size = CGSizeZero;
if ([collectionViewLayout respondsToSelector:@selector(itemSize)]) {
size = [(id)collectionViewLayout itemSize];
}
+ (Class)collectionViewCellClassForItemAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
id object = [model objectAtIndexPath:indexPath];
Class collectionViewCellClass = nil;
if ([object respondsToSelector:@selector(collectionViewCellClass)]) {
collectionViewCellClass = [object collectionViewCellClass];
}
if ([collectionViewCellClass respondsToSelector:@selector(sizeForObject:atIndexPath:collectionView:)]) {
CGSize cellSize = [collectionViewCellClass sizeForObject:object atIndexPath:indexPath collectionView:collectionView];
if (cellSize.width > 0 && cellSize.height > 0) {
size = cellSize;
}
}
return size;
return collectionViewCellClass;
}

@end
Expand Down

0 comments on commit 4ef57bc

Please sign in to comment.