简体   繁体   English

UICollectionView静态CustomCell重用

[英]UICollectionView Static CustomCell reuse

I have a problem i create UICollectionView with custom cell to display items. 我在创建带有自定义单元格的UICollectionView来显示项目时遇到问题。 But after refresh of the UICollectionView reusable cell populate for wrong index 但是在刷新UICollectionView可重用单元后,填充了错误的索引

UICollectionView before refresh. 刷新之前的UICollectionView。

在此处输入图片说明

UICollectionView after refresh. 刷新后的UICollectionView。

在此处输入图片说明

Code example of the reusable collection view cell. 可重用的收集视图单元格的代码示例。

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

}

This problem occurs because the cells will be reused. 发生此问题的原因是单元将被重用。 Cells are reused to improve the performance of the system. 单元被重用以改善系统性能。 If your table has 1000 cells, the system does not allocate 1000 cells but a much lower then reuse- 如果您的表有1000个单元格,则系统不会分配1000个单元格,但是会比重用时少得多-

Try with adding else clause to the if 尝试在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"];
}

Think it's reusing another cell (the balloon in this case) and is not setting anything for the first index cell. 认为它正在重用另一个单元格(在这种情况下为气球),并且没有为第一个索引单元格设置任何内容。 If you make an else statement to create a new camera cell, hopefully it will reappear. 如果您使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM