Add checkmark image in cell in Collectionview issue iOS

六月ゝ 毕业季﹏ 提交于 2019-12-12 04:09:33

问题


I tried to add checkmark image to cell but i have a problem, i just select 1 cell although i set the multiselection = YES for collectionview. And 1 more issue is when I scroll the collectionview, the selected cell is reuse, all checkmark images is incorrect. Below is the code i'm using.

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

        TDPhotoCell *photoCell =
        [collectionView dequeueReusableCellWithReuseIdentifier:TDPhotoCellIdentifier  forIndexPath:indexPath];

        TDPhoto *photo = self.photosArray[indexPath.row];

         [photoCell.photoImage sd_setImageWithURL:photo.imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
         {
             [activity removeFromSuperview];
         }];




        if (self.isSelectMode) {
            // check the selected photo
            if (self.selectedItemIndexPath != nil && [indexPath compare:self.selectedItemIndexPath] == NSOrderedSame) {
                UIImageView *CheckedImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 102, 102)];
                CheckedImage.tag = 118;
                CheckedImage.image = [UIImage imageNamed:@"Checked_Overlay"];
                [photoCell.contentView addSubview:CheckedImage];

            } else {
                //remove checkimage
                for(UIImageView *IMGview in photoCell.contentView.subviews)
                {
                    if (IMGview.tag == 118) {
                        [IMGview removeFromSuperview];
                    }

                }
            }
        }


        return photoCell;
}

and this is didSelectItemAtIndexPath():

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];

        if (self.selectedItemIndexPath)
        {
            // if we had a previously selected cell

            if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
            {
                // if it's the same as the one we just tapped on, then we're unselecting it

                self.selectedItemIndexPath = nil;

            }
            else
            {
                // if it's different, then add that old one to our list of cells to reload, and
                // save the currently selected indexPath

                [indexPaths addObject:self.selectedItemIndexPath];
                self.selectedItemIndexPath = indexPath;

            }
        }
        else
        {
            // else, we didn't have previously selected cell, so we only need to save this indexPath for future reference

            self.selectedItemIndexPath = indexPath;

        }

        // and now only reload only the cells that need updating

        [collectionView reloadItemsAtIndexPaths:indexPaths];

    }
}

Where is the incorrect in my code? Pls. give me some advice. Thanks in advance.

来源:https://stackoverflow.com/questions/25446070/add-checkmark-image-in-cell-in-collectionview-issue-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!