Permalink
Browse files

[collections] Rename pass #2.

Everything updated to use collection view.
  • Loading branch information...
1 parent 3b68bae commit cf6572cfef5e748a8b75330f9a12b7d92d409f30 @jverkoey committed Mar 28, 2013
@@ -59,12 +59,12 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
* the object to a cell it will return nil.
*
* @code
-- (UITableViewCell *)tableViewModel:(NICollectionViewModel *)tableViewModel
- cellForTableView:(UITableView *)tableView
+- (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel
+ cellForCollectionView:(UICollectionView *)collectionView
atIndexPath:(NSIndexPath *)indexPath
withObject:(id)object {
- UITableViewCell* cell = [NICollectionViewCellFactory tableViewModel:tableViewModel
- cellForTableView:tableView
+ UICollectionViewCell* cell = [NICollectionViewCellFactory collectionViewModel:collectionViewModel
+ cellForCollectionView:tableView
atIndexPath:indexPath
withObject:object];
if (nil == cell) {
@@ -74,7 +74,7 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
}
* @endcode
*/
-+ (UITableViewCell *)tableViewModel:(NICollectionViewModel *)tableViewModel cellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object;
++ (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel cellForCollectionView:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object;
/**
* Map an object's class to a cell's class.
@@ -83,59 +83,7 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
* mapping, the factory mapping will take precedence. This allows you to
* explicitly override the mapping on a case-by-case basis.
*/
-- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)cellClass;
-
-/**
- * Returns the height for a row at a given index path.
- *
- * Uses the heightForObject:atIndexPath:tableView: selector from the NICollectionViewCell 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 NICollectionViewCellObject protocol.
- *
- * If the cell returns a height of zero then tableView.rowHeight will be used.
- *
- * Example implementation:
- *
-@code
-- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return [self.cellFactory tableView:tableView heightForRowAtIndexPath:indexPath model:self.model];
-}
-@endcode
- *
- * @param tableView The table view within which the cell exists.
- * @param indexPath The location of the cell in the table view.
- * @param model The 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.
- */
-- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
-
-/**
- * Returns the height for a row at a given index path.
- *
- * Uses the heightForObject:atIndexPath:tableView: selector from the NICollectionViewCell 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 NICollectionViewCellFactory.
- *
- * If the cell returns a height of zero then tableView.rowHeight will be used.
- *
- * Example implementation:
- *
-@code
-- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return [NICollectionViewCellFactory tableView:tableView heightForRowAtIndexPath:indexPath model:self.model];
-}
-@endcode
- *
- * @param tableView The table view within which the cell exists.
- * @param indexPath The location of the cell in the table view.
- * @param model The 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.
- */
-+ (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model;
+- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)collectionViewCellClass;
@end
@@ -165,18 +113,15 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
@protocol NICollectionViewCellObject <NSObject>
@required
/** The class of cell to be created when this object is passed to the cell factory. */
-- (Class)cellClass;
+- (Class)collectionViewCellClass;
-@optional
-/** The style of UITableViewCell to be used when initializing the cell for the first time. */
-- (UITableViewCellStyle)cellStyle;
@end
/**
* The protocol for a cell created in the NICollectionViewCellFactory.
*
* Cells that implement this protocol are given the object that implemented the NICollectionViewCellObject
- * protocol and returned this cell's class name in @link NICollectionViewCellObject::cellClass cellClass@endlink.
+ * protocol and returned this cell's class name in @link NICollectionViewCellObject::collectionViewCellClass collectionViewCellClass@endlink.
*
* @ingroup TableCellFactory
*/
@@ -197,10 +142,10 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
* The following is an appropiate implementation in your tableView's delegate:
*
@code
--(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
+-(CGFloat)tableView:(UICollectionView *)collectionView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = tableView.rowHeight;
id object = [(NICollectionViewModel *)tableView.dataSource objectAtIndexPath:indexPath];
- id class = [object cellClass];
+ id class = [object collectionViewCellClass];
if ([class respondsToSelector:@selector(heightForObject:atIndexPath:tableView:)]) {
height = [class heightForObject:object atIndexPath:indexPath tableView:tableView];
}
@@ -213,7 +158,7 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
* methods on NICollectionViewCellFactory to achieve the same result. Using the above example allows you to
* customize the logic according to your specific needs.
*/
-+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView;
++ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UICollectionView *)collectionView;
@end
@@ -234,11 +179,11 @@ _model.delegate = (id)[NICollectionViewCellFactory class];
@interface NICollectionViewCellObject : NSObject <NICollectionViewCellObject>
// Designated initializer.
-- (id)initWithCellClass:(Class)cellClass userInfo:(id)userInfo;
-- (id)initWithCellClass:(Class)cellClass;
+- (id)initWithCellClass:(Class)collectionViewCellClass userInfo:(id)userInfo;
+- (id)initWithCellClass:(Class)collectionViewCellClass;
-+ (id)objectWithCellClass:(Class)cellClass userInfo:(id)userInfo;
-+ (id)objectWithCellClass:(Class)cellClass;
++ (id)objectWithCellClass:(Class)collectionViewCellClass userInfo:(id)userInfo;
++ (id)objectWithCellClass:(Class)collectionViewCellClass;
@property (nonatomic, readonly, NI_STRONG) id userInfo;
@@ -24,6 +24,7 @@
@interface NICollectionViewCellFactory()
@property (nonatomic, copy) NSMutableDictionary* objectToCellMap;
+@property (nonatomic, copy) NSMutableSet* registeredObjectClasses;
@end
@@ -39,28 +40,22 @@ @implementation NICollectionViewCellFactory
- (id)init {
if ((self = [super init])) {
_objectToCellMap = [[NSMutableDictionary alloc] init];
+ _registeredObjectClasses = [[NSMutableSet alloc] init];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-+ (UITableViewCell *)cellWithClass:(Class)cellClass
- tableView:(UITableView *)tableView
- object:(id)object {
- UITableViewCell* cell = nil;
++ (UICollectionViewCell *)cellWithClass:(Class)collectionViewCellClass
+ collectionView:(UICollectionView *)collectionView
+ indexPath:(NSIndexPath *)indexPath
+ object:(id)object {
+ UICollectionViewCell* cell = nil;
- NSString* identifier = NSStringFromClass(cellClass);
+ NSString* identifier = NSStringFromClass(collectionViewCellClass);
- cell = [tableView dequeueReusableCellWithIdentifier:identifier];
-
- if (nil == cell) {
- UITableViewCellStyle style = UITableViewCellStyleDefault;
- if ([object respondsToSelector:@selector(cellStyle)]) {
- style = [object cellStyle];
- }
- cell = [[cellClass alloc] initWithStyle:style reuseIdentifier:identifier];
- }
+ cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Allow the cell to configure itself with the object's information.
if ([cell respondsToSelector:@selector(shouldUpdateCellWithObject:)]) {
@@ -72,109 +67,74 @@ + (UITableViewCell *)cellWithClass:(Class)cellClass
///////////////////////////////////////////////////////////////////////////////////////////////////
-+ (UITableViewCell *)tableViewModel:(NICollectionViewModel *)tableViewModel
- cellForTableView:(UITableView *)tableView
++ (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel
+ cellForCollectionView:(UICollectionView *)collectionView
atIndexPath:(NSIndexPath *)indexPath
withObject:(id)object {
- UITableViewCell* cell = nil;
+ UICollectionViewCell* cell = nil;
// If this assertion fires then your app is about to crash. You need to either add an explicit
// binding in a NICollectionViewCellFactory object or implement the NICollectionViewCellObject protocol on this object and
// return a cell class.
- NIDASSERT([object respondsToSelector:@selector(cellClass)]);
+ NIDASSERT([object respondsToSelector:@selector(collectionViewCellClass)]);
// Only NICollectionViewCellObject-conformant objects may pass.
- if ([object respondsToSelector:@selector(cellClass)]) {
- Class cellClass = [object cellClass];
- cell = [self cellWithClass:cellClass tableView:tableView object:object];
+ if ([object respondsToSelector:@selector(collectionViewCellClass)]) {
+ Class collectionViewCellClass = [object collectionViewCellClass];
+ cell = [self cellWithClass:collectionViewCellClass collectionView:collectionView indexPath:indexPath object:object];
}
return cell;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (Class)cellClassFromObject:(id)object {
+- (Class)collectionViewCellClassFromObject:(id)object {
if (nil == object) {
return nil;
}
Class objectClass = [object class];
- Class cellClass = [self.objectToCellMap objectForKey:objectClass];
+ Class collectionViewCellClass = [self.objectToCellMap objectForKey:objectClass];
- BOOL hasExplicitMapping = (nil != cellClass && cellClass != [NSNull class]);
+ BOOL hasExplicitMapping = (nil != collectionViewCellClass && collectionViewCellClass != [NSNull class]);
- if (!hasExplicitMapping && [object respondsToSelector:@selector(cellClass)]) {
- cellClass = [object cellClass];
+ if (!hasExplicitMapping && [object respondsToSelector:@selector(collectionViewCellClass)]) {
+ collectionViewCellClass = [object collectionViewCellClass];
}
- if (nil == cellClass) {
- cellClass = [self.class objectFromKeyClass:objectClass map:self.objectToCellMap];
+ if (nil == collectionViewCellClass) {
+ collectionViewCellClass = [self.class objectFromKeyClass:objectClass map:self.objectToCellMap];
}
- return cellClass;
+ return collectionViewCellClass;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (UITableViewCell *)tableViewModel:(NICollectionViewModel *)tableViewModel
- cellForTableView:(UITableView *)tableView
+- (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel
+ cellForCollectionView:(UICollectionView *)collectionView
atIndexPath:(NSIndexPath *)indexPath
withObject:(id)object {
- UITableViewCell* cell = nil;
+ UICollectionViewCell* cell = nil;
- Class cellClass = [self cellClassFromObject:object];
+ Class collectionViewCellClass = [self collectionViewCellClassFromObject:object];
// If this assertion fires then your app is about to crash. You need to either add an explicit
// binding in a NICollectionViewCellFactory object or implement the NICollectionViewCellObject protocol on this object and
// return a cell class.
- NIDASSERT(nil != cellClass);
+ NIDASSERT(nil != collectionViewCellClass);
- if (nil != cellClass) {
- cell = [[self class] cellWithClass:cellClass tableView:tableView object:object];
+ if (nil != collectionViewCellClass) {
+ cell = [[self class] cellWithClass:collectionViewCellClass collectionView:collectionView indexPath:indexPath object:object];
}
return cell;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)cellClass {
- [self.objectToCellMap setObject:cellClass forKey:(id<NSCopying>)objectClass];
-}
-
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
- CGFloat height = tableView.rowHeight;
- id object = [model objectAtIndexPath:indexPath];
- Class cellClass = [self cellClassFromObject:object];
- if ([cellClass respondsToSelector:@selector(heightForObject:atIndexPath:tableView:)]) {
- CGFloat cellHeight = [cellClass heightForObject:object
- atIndexPath:indexPath tableView:tableView];
- if (cellHeight > 0) {
- height = cellHeight;
- }
- }
- return height;
-}
-
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-+ (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NICollectionViewModel *)model {
- CGFloat height = tableView.rowHeight;
- id object = [model objectAtIndexPath:indexPath];
- Class cellClass = nil;
- if ([object respondsToSelector:@selector(cellClass)]) {
- cellClass = [object cellClass];
- }
- if ([cellClass respondsToSelector:@selector(heightForObject:atIndexPath:tableView:)]) {
- CGFloat cellHeight = [cellClass heightForObject:object
- atIndexPath:indexPath tableView:tableView];
- if (cellHeight > 0) {
- height = cellHeight;
- }
- }
- return height;
+- (void)mapObjectClass:(Class)objectClass toCellClass:(Class)collectionViewCellClass {
+ [self.objectToCellMap setObject:collectionViewCellClass forKey:(id<NSCopying>)objectClass];
}
@end
@@ -230,7 +190,7 @@ + (id)objectFromKeyClass:(Class)keyClass map:(NSMutableDictionary *)map {
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface NICollectionViewCellObject()
-@property (nonatomic, assign) Class cellClass;
+@property (nonatomic, assign) Class collectionViewCellClass;
@property (nonatomic, NI_STRONG) id userInfo;
@end
@@ -240,35 +200,35 @@ @interface NICollectionViewCellObject()
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NICollectionViewCellObject
-@synthesize cellClass = _cellClass;
+@synthesize collectionViewCellClass = _collectionViewCellClass;
@synthesize userInfo = _userInfo;
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (id)initWithCellClass:(Class)cellClass userInfo:(id)userInfo {
+- (id)initWithCellClass:(Class)collectionViewCellClass userInfo:(id)userInfo {
if ((self = [super init])) {
- _cellClass = cellClass;
+ _collectionViewCellClass = collectionViewCellClass;
_userInfo = userInfo;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (id)initWithCellClass:(Class)cellClass {
- return [self initWithCellClass:cellClass userInfo:nil];
+- (id)initWithCellClass:(Class)collectionViewCellClass {
+ return [self initWithCellClass:collectionViewCellClass userInfo:nil];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-+ (id)objectWithCellClass:(Class)cellClass userInfo:(id)userInfo {
- return [[self alloc] initWithCellClass:cellClass userInfo:userInfo];
++ (id)objectWithCellClass:(Class)collectionViewCellClass userInfo:(id)userInfo {
+ return [[self alloc] initWithCellClass:collectionViewCellClass userInfo:userInfo];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-+ (id)objectWithCellClass:(Class)cellClass {
- return [[self alloc] initWithCellClass:cellClass userInfo:nil];
++ (id)objectWithCellClass:(Class)collectionViewCellClass {
+ return [[self alloc] initWithCellClass:collectionViewCellClass userInfo:nil];
}
Oops, something went wrong.

0 comments on commit cf6572c

Please sign in to comment.