0

我有一个问题,我创建了UICollectionView与自定义单元格来显示项目。但刷新后的UICollectionView可重复使用的单元填充错误索引UICollectionView Static CustomCell reuse

刷新前的UICollectionView。

enter image description here

刷新后UICollectionView。可重复使用的集合视图细胞的

enter image description here

代码示例。

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

    GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    if (indexPath.item != 0) 
    { 
     [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
    } 

    return cell; 

} 

回答

2

发生此问题的原因是单元格将被重新使用。单元被重用来改善系统的性能。如果你的表有1000个细胞时,系统不分配1000个细胞,但与添加else子句的if

if (indexPath.item != 0) 
{ 
    [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
} 
else 
{ 
    //Set your cell at index 0 with your camera image 
    [cell setCollectionItem:@"camera-image"]; 
} 
2

一个低得多然后reuse-

尝试认为它是重复使用的另一个小区(气球情况),并没有为第一个索引单元设置任何内容。如果您创建一个else语句来创建一个新的摄像头单元,希望它会重新出现。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    if (indexPath.item != 0) { 
     [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
    } else { 
     // Set camera item here 
    } 
    return cell; 
}