ICollectionView收到了一个索引路径不存在的单元格的

ICollectionView收到了一个索引路径不存在的单元格的

本文介绍了UICollectionView收到了一个索引路径不存在的单元格的布局属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used UICollection view to show items in grid layout.

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

For data source I have use 5*5 dimensional array.

此外,我在numberOfItems中返回5,numberOfSections返回5。

And also I am returning 5 for numberOfItems in section and 5 for numberOfSections.

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

Then also my app is getting crashed with following error:

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

'UICollectionView recieved layout attributes for a cell with an index path that does not exist: {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);

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

任何人都可以解决这个问题吗?
在此先感谢。

Can anyone solve this problem?Thanks in advance.

推荐答案

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

The MultipleLineLayout was originally written for infinite scrolling, so there was a problem with that implementation for your use. It should look like this,

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

这篇关于UICollectionView收到了一个索引路径不存在的单元格的布局属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:08