0
我正在嘗試使用自定義筆尖創建UICollectionView。要做到這一點,我在做我與UITableView中做同樣的事情:嘗試創建UICollectionViewCell時聲明失敗
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;
}
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我得到*** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:1367
感謝您的幫助!
您需要檢查' - (空)registerNib:(UINib *)筆尖forCellWithReuseIdentifier:(的NSString *)identifier'上'UICollectionView' –
還應該有一個兩行高於或低於該給出更具體的原因。 – Kevin