【问题标题】:UICollectionView Assertion failure in createPreparedCellForItemAtIndexPathcreatePreparedCellForItemAtIndexPath 中的 UICollectionView 断言失败
【发布时间】:2017-08-08 09:53:28
【问题描述】:

我的代码很简单,就是想创建一个流式布局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);
}

这是错误

*** -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] 中的断言失败, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.9.1/UICollectionView.m:2115

【问题讨论】:

  • 你正在使用storyboard或xib??
  • @ShabbirAhmad 两者都没有,只有代码
  • 只需将单元格从队列中取出: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

标签: ios cocoa-touch uicollectionview


【解决方案1】:

尝试使用集合视图单元的出队可重用性:

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

【讨论】:

    【解决方案2】:

    如文档所述

    集合视图要求您始终将视图出列,而不是在代码中显式创建它们

    这与 UITableView 不同,只是一直对单元格进行 deque

    错误:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2012-09-17
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多