在Collectionview问题iOS中的单元格中添加复选标记图像

我试图添加复选标记图像单元格,但我有一个问题,我只是select1单元格,虽然我设置multiselection = YES为collectionview。 还有另外一个问题是当我滚动collectionview时,选中的单元格被重用,所有复选标记图片都是不正确的。 以下是我使用的代码。

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

这是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]; } } 

我的代码在哪里不正确? PLS。 给我一些build议。 提前致谢。