我面临同样的问题 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/