ios - UICollectionView 中的选择和取消选择问题

标签 ios objective-c uicollectionview uiimageview uicollectionviewcell

我面临同样的问题 Multiple selections Issue

这是我完成的代码。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RCCollectionCell* cell = [_channelCollectionView dequeueReusableCellWithReuseIdentifier:@"RC" forIndexPath: indexPath];
    cell.layer.cornerRadius = 5.0f;
    cell.layer.borderWidth=1.0f;
    cell.layer.borderColor=[UIColor lightGrayColor].CGColor;
    if (indexPath.row < [_fC count]){
        [cell setChannel:_favoriteChannels[indexPath.row]];
        [_channelCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    } else {
        //NSLog(@"Error cell %d requested only %d channels in incident", (int)indexPath.row, (int)[_incident.channels count]);
    }
    return cell;
}  
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    RCCollectionCell* cell = [_channelCollectionView dequeueReusableCellWithReuseIdentifier:@"RC" forIndexPath: indexPath];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RCCollectionCell* cell = (RCCollectionCell*)[collectionView cellForItemAtIndexPath:indexPath];
    Channel* channel = [self getChannelForIndexPath:indexPath];
    if ([_upload.channels containsObject:channel.uuid]) {
        [_upload.channels removeObject:channel.uuid];
        cell.selectedImage.hidden = YES;

    } else {
        [self.view makeToast:channel.name duration:1.5 position:CSToastPositionCenter];

        [_upload.channels addObject:channel.uuid];
        cell.selectedImage.hidden = NO;
    }
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}  

我的问题是 cell.selectedImage.hidden = NO; 当我单击任何单元格并滚动 collectionview 时,我可以看到另一个单元格也受到 selectedimage.hidden = no 的影响。

请给我一些解决方案来解决这个问题。

提前致谢。

编辑:

selectedImage 是我用来选中和取消选中单元格的复选标记图像。

最佳答案

首先您创建一个 NSMutableArray 来存储选定的 collectionview 索引路径。

NSMutableArray *SelectedIndexes = [[NSMutableArray alloc]init];

并在didselect的if条件方法中添加indexpath

[SelectedIndexes addObject:indexPath];

并删除 didselect 方法的“else”条件中的 indexpath。

if ([SelectedIndexes containsObject:indexPath]) {
    [SelectedIndexes removeObject:indexPath];
}

在您的 cellForItemAtIndexPath 方法中检查选定的索引路径。

if ([SelectedIndexes containsObject:indexPath]) {
   cell.selectedImage.hidden = YES;
}
else {
    cell.selectedImage.hidden = NO;
}

关于ios - UICollectionView 中的选择和取消选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57195109/

相关文章:

ios - Tesseract OCR 不会忽略黑名单字符

ios - 运行时有吸引力的 iOS UIButtons

ios - 我们可以使用较低版本的 pay pal sdk 和最新的 xcode 和 swift 版本吗

ios - 为什么 NSDate 类型的变量没有在初始化程序中初始化?

ios - objective-c - SKScene 的动画背景颜色

objective-c - 如何从代码中选择 UICollectionView 的项目/单元格

ios - UITableViewCells 中的 UIImageViews 正在缓存的单元格中重新创建自己

iOS 9 不显示 UIPageViewController 全屏

ios - 如何防止在 cellForItemAt 中重新加载 UICollectionViewCells?

ios - 如何将 Collection View 单元格大小设置为与 iOS 中的 Collection View 完全相等?