Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:?

Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]

I got this error since IOS 7. It works fine in IOS 6.

I look around the web and I found this:

http://forums.xamarin.com/discussion/8233/ios-7-crash-in-collectionview

However, the solution doesn't make sense

I figured it out. I had incorrectly used CollectionView.RegisterClassForCell. Apparently I was supposed to use CollectionView.RegisterNibForCell when setting up the viewcontroller. That fixed the problem. iOS 6 must have been more forgiving.

Could any of you give me a hint on this bug

Symptomps:

  1. Crash
  2. Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
  3. Works in IOS6 and not IOS 7

The code I am suspicious with is this:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize sz = [BGCollectionViewCellImage defaultSize];
    return sz;
}

But it seems to be too ordinary.

sz is simply CGSize of 100*120

Another is this:

- (BGCollectionViewCellImage *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BGCollectionViewCellImage * cell = [[BGCollectionViewCellImage alloc]init];
    Image * img= self.arImages[indexPath.row];
    [cell setImg:img andIndex:indexPath.row+1];

    return cell;
}

Maybe I should use dequeue something like in UITableViewCell

I got another hint. If I tried to dequeue some cell, I got this:

2013-10-14 21:18:34.346 domainname[24667:a0b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier BGCollectionViewCellImage - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack: (

Looks like I got to register something first.

like image 637
user4951 Avatar asked Oct 14 '13 13:10

user4951


2 Answers

In case anyone hits this, another possibility is if you forget to return cell; at the end of the collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method.

like image 127
Liron Avatar answered Oct 08 '22 17:10

Liron


You need to register your cell using one of the relevant registration methods provided in the UICollectionView Class Reference

- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

The relevant one of these only needs to be called once per instance of a UICollectionView.

like image 11
Tim Avatar answered Oct 08 '22 18:10

Tim