【问题标题】:didSelectItemAtIndexPath / didDeselectItemAtIndexPath in PSTCollectionView in iOSiOS 中 PSTCollectionView 中的 didSelectItemAtIndexPath / didDeselectItemAtIndexPath
【发布时间】:2015-03-15 22:31:05
【问题描述】:

我正在使用UICollectionView 使用PSTCollectionView 图书馆。我必须创建一个用户可以选择和取消选择的网格 通过点击UICollectionViewCell 获取图像。我必须显示复选框 如果选择了单元格,则为图像。如果单元格是 uncheckedBox 图像 取消选择。我可以选择cell 并显示复选框图像。并且 也可以取消选择。但是当我选择下一个cell时,前一个被取消选中 cell 也被选中并显示复选框图像。这是我在UICollectionViewCell子类中声明的方法

 -(void)applySelection{
    if(_isSelected){
        _isSelected=FALSE;
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"unchecked_edit_image.png"];
    }else{
        _isSelected=TRUE;
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"checked_edit_image.png"];
    }
}

这是我的didSelectItemAtIndexPath 代码和 didDeselectItemAtIndexPath

- (void)collectionView:(PSTCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelect method called");
    FriendImageCell *cell = (FriendImageCell*)[imageGrid cellForItemAtIndexPath:indexPath];
        [selectedImages addObject:[[list objectAtIndex:indexPath.item] objectForKey:@"thumbnail_path_150_150"]];
         [cell applySelection];

}

- (void)collectionView:(PSTCollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did deselect called");
    FriendImageCell *cell = (FriendImageCell*)[imageGrid cellForItemAtIndexPath:indexPath];
    [selectedImages removeObjectAtIndex:indexPath.item];
    [cell setSelected:NO];
    [cell applySelection];
}

谁能让我理解我的代码有什么问题?制作 如果我做错了什么,我会纠正。尝试了很多答案 堆栈溢出但没有任何效果。任何帮助,将不胜感激。 提前致谢。

【问题讨论】:

  • 你的indexPath.item 是什么?我在NSIndexPath reference 中找不到它。
  • UICollectionView 中,单元格被视为项目。

标签: ios objective-c uicollectionview uicollectionviewcell pstcollectionview


【解决方案1】:

经过几天的反复讨论。我想我终于明白你的问题到底是什么了。您一定忘记将allowsMultipleSelection 设置为YES。因此,只要选择新的单元格,您之前的单元格就会取消选择。

允许多重选择

此属性控制是否可以同时选择多个项目。此属性的默认值为 NO。

在我之前的回答中,我还建议您创建自己的布尔数组来跟踪所选项目。但是,我刚刚意识到您不必这样做。 indexPathsForSelectedItems 为您提供一组选定的索引路径。

indexPathsForSelectedItems

NSIndexPath 对象的数组,每个对象对应一个选定的项目。如果没有选定项,则此方法返回一个空数组。

事实上,您甚至不必实现didSelectItemAtIndexPathdidDeselectItemAtIndexPath。默认情况下,这两个委托方法会为您调用setSelected:。因此,更合适的做法是将您的applySelection 代码移入setSelected

覆盖自定义UICollectionViewCell 中的setSelected: 方法。

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];

    // Change your UI
    if(_isSelected){
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"unchecked_edit_image.png"];
    }else{
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"checked_edit_image.png"];
    }
}

【讨论】:

  • 如何创建一个布尔数组?
  • 是的,我只想说它是一个布尔数组。让我添加“如何创建布尔数组?”对于答案,在 cmets 中解释它有点长..
  • 感谢您的回答,但它不允许选择多个单元格。
  • 确实如此,这就是我们用于应用的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多