Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[collections] Provide access to the cell factory classes rather than …

…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...
commit 4ef57bcf841ed6808c8a0da66d99140943af6d5d 1 parent 297bf27
@jverkoey authored
View
88 src/collections/src/NICollectionViewCellFactory.h
@@ -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
@@ -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
/**
View
29 src/collections/src/NICollectionViewCellFactory.m
@@ -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

0 comments on commit 4ef57bc

Please sign in to comment.
Something went wrong with that request. Please try again.