rpstw
rpstw

Reputation: 1712

UICollectionView Assertion failure in createPreparedCellForItemAtIndexPath

my code is simple,just want to create a flow layout UIColloctionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 7;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [UICollectionViewCell new];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH / 2 - 5, 160);
}

and this is the error

*** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.9.1/UICollectionView.m:2115

Upvotes: 0

Views: 1635

Answers (2)

Hakikat Singh
Hakikat Singh

Reputation: 285

Try using dequeue reusability of the collection view cell:

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}

Upvotes: 1

rpstw
rpstw

Reputation: 1712

as the documentation said

the collection view requires that you always dequeue views, rather than create them explicitly in your code

this is different from UITableView , just deque the cell all the time

wrong:

UICollectionViewCell *cell = [UICollectionViewCell new];
return cell;

Upvotes: 0

Related Questions