我真的,真的想为我的 UICollectionView 提供“静态”单元格,就像 UITableView 的过去一样。但我知道,我们优秀的男孩和女孩必须将dequeueReusableCellWithReuseIdentifier:forIndexPath:
其用作我们细胞的工厂。因此,我提出了以下方案,并要求对其潜力进行反馈。到目前为止,它对我有用,但我害怕一个未知的陷阱。
#import "MyCellClass.h"
@implementation MyViewController {
MyCellClass *_cell0; // etc - many are possible. could store in array
}
-(void)viewDidLoad {
// assume MyCellClass has a +nib and +reuseId. The reader understands.
[_collectionView registerNib:[MyCellClass nib] forCellWithReuseIdentifier:[MyCellClass reuseId]];
}
-(void)viewDidAppear:animated {
// this is where the fun begins. assume proper counts coming from the datasource
NSIndexPath *indexPath = [NSIndexPath indexPathWithRow:0 inSection:0];
_cell0 = [[self collectionView] dequeueReusableCellWithReuseIdentifier:[MyCellClass reuseId] forIndexPath:indexPath];
// etc
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) return _cell0;
// etc
}
我故意掩盖了诸如配置单元格之类的细节。疯狂?天才?只是现状?到目前为止它似乎工作,但我担心,不知何故,Apple 期望dequeue
从内部调用cellForPath
。有什么想法吗?