【问题标题】:Selection and unselection isselection issue in UICollectionViewUICollectionView 中的选择和取消选择问题
【发布时间】:2019-12-03 07:51:04
【问题描述】:

我也面临同样的问题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;,当我单击任何单元格并滚动集合视图时,我可以看到另一个单元格也受到 selectedimage.hidden = no 的影响。

请给我一些解决此问题的解决方案。

提前致谢。

编辑:

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

【问题讨论】:

  • if部分你需要写cell.selectedImage.hidden = YES;
  • 使用 UICollectionView 选择的目的是什么?我没有看到任何依赖于所选项目的逻辑
  • @dahiya_boy 我无法理解。你能详细说明一下吗?
  • @TiranUt 我正在使用集合视图来显示水平列表视图,其中包括图像文本和复选标记图像。
  • if条件写什么?

标签: ios objective-c uicollectionview uiimageview uicollectionviewcell


【解决方案1】:

首先你创建一个 NSMutableArray 来存储选择的collectionview indexpaths。

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

并在 didselect 方法的 if 条件中添加索引路径

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

【讨论】:

  • 非常感谢,伙计,它正在工作!我在 if ([SelectedIndexes containsObject:indexPath]) { cell.selectedImage.hidden = YES; } 其他 { cell.selectedImage.hidden = NO;而不是 YES 我将其更改为 NO 和 NO 为 YES
  • 好吧。好的。快乐编码。
猜你喜欢
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多