【问题标题】:UICollectionView recieved layout attributes for a cell with an index path that does not existUICollectionView 收到具有不存在索引路径的单元格的布局属性
【发布时间】:2014-07-16 15:33:20
【问题描述】:

我使用 UICollection 视图在网格布局中显示项目。

对于数据源,我使用 5*5 维数组。

我还为 section 中的 numberOfItems 返回 5,为 numberOfSections 返回 5。

然后我的应用程序也因以下错误而崩溃:

'UICollectionView 收到一个单元格的布局属性,其索引路径不存在:{length = 2, path = 0 - 5}'

//////===viewcontroller.m==///////
- (void)viewDidLoad {
    self.theData = @[@[@"1",@"2",@"3",@"4",@"5"], @[@"6",@"7",@"8",@"9",@"10"],@[@"11",@"12",@"13",@"14",@"15"],@[@"16",@"17",@"18",@"19",@"20"],@[@"21",@"22",@"23",@"24",@"25"]];
    MultpleLineLayout *layout = [[MultpleLineLayout alloc] init];
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.view.backgroundColor = [UIColor blackColor];
    [self.collectionView registerClass:[DataCell class] forCellWithReuseIdentifier:@"DataCell"];
    [self.collectionView reloadData];
}


- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return 5;

}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    DataCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"DataCell" forIndexPath:indexPath];
    cell.label.text = self.theData[indexPath.section ][indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   // UICollectionViewCell *item = [collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"%@",indexPath);

}
///////////////////////

谁能解决这个问题? 提前致谢。

【问题讨论】:

  • 可能是MultpleLineLayout 的问题?这是你写的布局吗?
  • @KyleTruscott 谢谢,你说得对,这是与多线布局有关的问题。
  • 在我的情况下,我返回了错误的方法上的项目数:collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) 例如:我的对象中有 9 个项目,但我返回了 18...错误类似于“长度 = 8,路径 = 0 - 18”,但出现在这里是因为我使用了自定义的 collectionViewLayout。 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

MultipleLineLayout 最初是为无限滚动而编写的,因此该实现存在问题供您使用。它应该是这样的,

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {

    NSMutableArray* attributes = [NSMutableArray array];
    for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
        for (NSInteger j=0 ; j < [self.collectionView numberOfItemsInSection:i]; j++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}

【讨论】:

  • 非常感谢,你拯救了我的一天。我试图使用 uitableview 做网格,但它变得非常困难。
【解决方案2】:

以防其他人在 Google 上发现这个问题 - 我收到了同样的错误,索引路径非常有趣:

'NSInternalInconsistencyException',原因:'UICollectionView 收到 具有不存在的索引路径的单元格的布局属性: {长度 = 2,路径 = 0 - 0}'

我只是忘记连接集合视图的数据源并委托给 Interface Builder 中的视图控制器。哦!

【讨论】:

    【解决方案3】:

    它对我有用。

    
        collectionView.reloadData()
        collectionView.collectionViewLayout.invalidateLayout()
    
    

    因为reloadData时Cell的Autolayout缓存了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多