Ios 尝试创建UICollectionViewCell时断言失败
我正在尝试使用自定义nib创建UICollectionView。为此,我正在使用UITableView执行相同的操作: MyCollectionViewController.mIos 尝试创建UICollectionViewCell时断言失败,ios,uicollectionview,uicollectionviewcell,reuseidentifier,Ios,Uicollectionview,Uicollectionviewcell,Reuseidentifier,我正在尝试使用自定义nib创建UICollectionView。为此,我正在使用UITableView执行相同的操作: MyCollectionViewController.m - (void)viewDidLoad { [super viewDidLoad]; myCollectionView.delegate = self; myCollectionView.dataSource = self; } - (UICollectionViewCell *)collec
- (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;
}
MyCollectionCell.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上,我在-[UICollectionView\u createPreparedCellForItemAtIndexPath:WithLayoutAttribute:applyAttributes:],/SourceCache/UIKit\u Sim/UIKit-2903.23/UICollectionView.m:1367中获得了断言失败
谢谢你的帮助
- (void)viewDidLoad {
[super viewDidLoad];
myCollectionView.delegate = self;
myCollectionView.dataSource = self;
[collectionView registerNib:[UINib nibWithNibName:@"MyCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MyCollectionCell"];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *rid = @"MyCollectionCell";
return (MyCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:rid forIndexPath:indexPath];
}
然后在MyCollectionCell.m中,去掉所有的类方法,从awakeFromNib调用initCell
。i、 e:
-(void)awakeFromNib {
[super awakeFromNib];
[self initCell];
}
您需要在UICollectionView
上签出-(void)registerNib:(UINib*)nib forCellWithReuseIdentifier:(NSString*)identifier
在UICollectionView
的上方或下方应该有另一行或两行给出更具体的原因。