UICollectionView静态CustomCell重用

时间:2015-04-03 15:09:23

标签: ios objective-c iphone uicollectionview uicollectionviewcell

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

刷新前的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 个答案:

答案 0 :(得分:2)

发生此问题是因为将重用单元格。重复使用单元以改善系统的性能。如果您的表有1000个单元格,系统不会分配1000个单元格,但会低于重复使用 -

尝试在if

中添加else子句
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"];
}

答案 1 :(得分:2)

认为它正在重复使用另一个单元格(在这种情况下为气球)并且没有为第一个索引单元格设置任何内容。如果你创建一个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;
}
相关问题