我试图将复选标记图像添加到单元格但我有问题,我只选择1个单元格,虽然我为collectionview设置了multiselection = YES。还有一个问题是当我滚动收集视图时,所选单元格被重用,所有复选标记图像都不正确。以下是我正在使用的代码。
- (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。给我一些建议。提前谢谢。