0

I'm trying to create a UICollectionView with a customize nib. To do that, I'm doing the same thing I'm doing with 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;
}

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
{
}

I get an error

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2380.17/UICollectionView.m:2249

on this line

return (MyCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:rid forIndexPath:indexPath];

I don't really get what's happening. It hase something to do with the reuse because if I call only [MyCollectionCell newCell]; it works. Actually It works only on iOS6. On iOS7 I get *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:1367

Thanks for your help !

Nicolas Roy
  • 3,773
  • 5
  • 27
  • 42
  • You need to check out `- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier` on `UICollectionView` – Mike Pollard Feb 07 '14 at 16:40
  • There should be another line or two above or below that that gives a more specific cause. – Kevin Feb 07 '14 at 16:41

1 Answers1

0
- (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];
}

Then in MyCollectionCell.m do away with all your class methods and call initCell from awakeFromNib. i.e.:

-(void)awakeFromNib {
    [super awakeFromNib];
    [self initCell];
}
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46