我正在尝试使用自定义笔尖创建 UICollectionView。为此,我正在做与 UITableView 相同的事情:
MyCollectionViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
myCollectionView.delegate = self;
myCollectionView.dataSource = self;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionCell *cell = [MyCollectionCell newCellOrReuse:collectionView atIndexPath:indexPath];
return cell;
}
我的CollectionCell.m
+ (MyCollectionCell *)newCell
{
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"MyCollectionCell" bundle:nil];
MyCollectionCell *cell = (MyCollectionCell *)vc.view;
[cell initCell];
return cell;
}
+ (MyCollectionCell *)reuse:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)indexPath
{
NSString *rid = @"MyCollectionCell";
return (MyCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:rid forIndexPath:indexPath];
}
+ (MyCollectionCell *)newCellOrReuse:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)indexPath
{
MyCollectionCell *cell = [MyCollectionCell reuse:collectionView atIndexPath:indexPath];
if (!cell) cell = [MyCollectionCell newCell];
return cell;
}
- (void) initCell
{
}
我收到一个错误
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2380.17/UICollectionView.m:2249
在这条线上
return (MyCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:rid forIndexPath:indexPath];
我真的不明白发生了什么。它与重用有关,因为如果我只调用[MyCollectionCell newCell];
它就可以了。实际上它只适用于iOS6。在 iOS7 我得到 *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:1367
谢谢你的帮助 !