简体   繁体   中英

UICollectionView Static CustomCell reuse

I have a problem i create UICollectionView with custom cell to display items. But after refresh of the UICollectionView reusable cell populate for wrong index

UICollectionView before refresh.

在此处输入图片说明

UICollectionView after refresh.

在此处输入图片说明

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-

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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