UICollectionview 单元格选择

2024-05-27

我制作了一个图像网格,为了显示其选择,我在选择时为图像绘制了边框。但问题是,当我选择顶部的一些图像并向下滚动图像网格时,底部的其他一些图像似乎也被选择了。下面是我的代码片段:

UINib *cellNib = [UINib nibWithNibName:@"collectionCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cellCV"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(95, 95)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

[self.collectionView setCollectionViewLayout:flowLayout];

上面已经在viewDidLoad中添加了nib中设计的集合视图单元格。

并实施了以下代表:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{   
selectedImageIndex = indexPath.row;
[collectionView reloadData]; 
}

-(CollectionCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *img = [imageArray objectAtIndex:indexPath.row];

static NSString *cellIdentifier = @"cellCV";
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:   
cellIdentifier forIndexPath:indexPath];
cell.imageView.image = img;
cell.imageView.tag = indexPath.row;
UIImageView *imgView = (UIImageView *)[cell viewWithTag:indexPath.row];
if (indexPath.row == selectedImageIndex) {
    imgView.layer.borderWidth =  4.0;
    imgView.layer.borderColor = [UIColor redColor].CGColor;
    NSLog(@"selected indexpath: %d", indexPath.row);
}
else {
    imgView.layer.borderWidth =  0.0;
    imgView.layer.borderColor = nil;
}
return cell;    
}

我猜想重复使用该单元会出现问题,但不确定,也无法提出解决该问题的想法。 等待任何形式的帮助和建议。

提前致谢。


我不明白为什么会发生这种情况。我不认为问题在于使用row vs item,虽然你真的应该使用item。不过,我可以想象,如果您的收藏视图有多个section,只看row/item但忽略section将是一个问题(即它会选择相同的item数在every section).

为了快刀斩乱麻,我建议保存NSIndexPath选定的项目,然后将其用作比较的基础。这也使得在以下方面进行优化变得容易didSelectItemAtIndexPath。无论如何,首先定义你的属性:

@property (nonatomic, strong) NSIndexPath *selectedItemIndexPath;

然后实施cellForItemAtIndexPath and didSelectItemAtIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.imageView.image = ...

    if (self.selectedItemIndexPath != nil && [indexPath compare:self.selectedItemIndexPath] == NSOrderedSame) {
        cell.imageView.layer.borderColor = [[UIColor redColor] CGColor];
        cell.imageView.layer.borderWidth = 4.0;
    } else {
        cell.imageView.layer.borderColor = nil;
        cell.imageView.layer.borderWidth = 0.0;
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // always reload the selected cell, so we will add the border to that cell

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

顺便说一句,请注意,我不会乱搞tag财产(我认为没有价值)。另请注意,我不是重新加载整个集合视图,而是仅重新加载选定的单元格(如果有先前选定的单元格,则也是该单元格),这应该更有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UICollectionview 单元格选择 的相关文章

随机推荐

  • 如何阻止gradle每次都下载

    我已经在 Ubuntu 16 04 中安装了 gradle 这是输出gradle v Gradle 2 10 Build time 2016 01 26 15 17 49 UTC Build number none Revision UNK
  • 实体框架中对象属性中的 NULL 值

    Tables Article Author Comment 1篇文章和1位作者可以有 评论 数据库中有 1 篇文章 1 位作者和 1 条评论 问题是 该代码 myBD my bd new myBD var articles by bd Ar
  • 系数函数速度慢

    请考虑 Clear x expr Sum x i i 15 30 CoefficientList expr x Timing Coefficient Expand expr x 234 Timing Coefficient expr x 2
  • 类型错误:“生成器”对象没有属性“__getitem__”

    我编写了一个应该返回字典的生成函数 但是当我尝试打印字段时出现以下错误 print row2 SearchDate TypeError generator object has no attribute getitem 这是我的代码 fro
  • numpy.nan_to_num - 'nan' 关键字无法识别

    当您尝试使用以下代码将 nan 值替换为自定义数值时 np nan to num exp allowance nan 9999 99 它产生以下错误 typeerror nan to num got an unexpected keywor
  • 丰富:数据表行跨度问题

    我需要创建一个 rich dataTable 甚至扩展 具有以下功能 我有一个公司类 其中包含产品对象的集合 我想展示下表 我仍然没有弄清楚如何使用子表执行此操作 在所有示例中 我发现子表具有与主表完全相同的列 据推测 我需要在前两列中使用
  • 如何使用 Swift 将文本复制到剪贴板/粘贴板

    我正在寻找一个干净的示例 说明如何将文本复制到 iOS 剪贴板 然后可以在其他应用程序中使用 粘贴 该功能的好处是可以快速复制文本 无需传统文本复制的标准文本突出显示功能 我假设关键课程位于UIPasteboard 但在中找不到相关区域他们
  • 使用 Spring Batch 将文件中的日期解析为 LocalDateTime

    我正在尝试使用 Spring Batch 读取包含日期的 CSV 文件 但在将日期解析为LocalDateTime Object 字段 日期 上的对象 目标 中的字段错误 拒绝值 2017 07 20 04 15 25 0 代码 typeM
  • Java:如何检测(并更改?)System.console 的编码?

    我有一个在控制台上运行的程序 其变音符号和其他特殊字符在 Mac 上以 的形式输出 这是一个简单的测试程序 public static void main String args System out println h h System
  • Mongoose - 遍历对象

    在 node js 上使用 mongoose 我试图找到玩家 game players id 等于我传递的 id 的所有游戏 Schema var Game mongoose Schema id String date type Date
  • Eclipse CDT C++20 支持

    有没有办法在 Eclipse 2020 03 CDT 9 11 中启用对 C 20 功能的支持 我的意思是摆脱不正确的语法突出显示 例如忽略新添加的关键字 如 concept requires 以及 C 20 标准中定义的其他关键字 我有一
  • 如何获取 res.drawable 文件夹的路径来复制文件?

    我正在编写我的应用程序AndroidStudio 我的里面有gif文件drawable gifs文件夹 我希望将该文件复制到MediaStore Images Media单击按钮后的文件夹 目前 即使使用发布的一些答案 我也无法获取我的 g
  • 将 NULL 传递给 SSRS URL 访问参数?

    使用 URL 访问将参数传递给 SSRS 报告时如何指定 NULL 值 即当使用表格时 http server ReportServer Pages ReportViewer aspx Report Parameter value 使用语法
  • selenium 没有找到合适的方法,直到(ExpectedCondition)

    这是有线的问题 我导入的项目运行 100 几个月前 今天我已将其与依赖项一起导入 但存在问题WebDriverWait 这是我的代码 WebDriverWait driverWait new WebDriverWait driver 100
  • 在 Composite WPF (Prism) 中,我应该如何对我的控制器进行单元测试?

    我正在使用一个模块构建一个基本的复合 WPF Shell 我想对我的模块进行单元测试 显然 Composite WPF 以一种易于单元测试的方式模块化我的代码 下面是我想要进行单元测试的代码 它驻留在我的模块的控制器中 请注意标准复合 WP
  • 一次更改多个表

    我正在尝试一次更改多个 SQL Server 2008 R2 表 这是我的代码 use DatabaseName go Declare SchemaUsed varchar 20 dbo create table Tables TableN
  • 使用 javascript 将子元素添加到父元素

    我正在尝试添加一个子元素 to a 父元素如下 li要添加到ul 单击 Enter 按钮或按下键盘上的 Enter 键时 会生成一个新的li and delete按钮应该添加到ul 我的代码无法正常工作 有人可以帮我解决这个问题吗 HTML
  • 如何使用内容类型:多部分/相关

    我想向用户发送一个二进制文件 并在浏览器上显示一些 HTML 我正在使用 apache2 的 CGI BIN 脚本 我发现此示例位于第 4 页 http www ietf org rfc rfc2387 txt 该链接还显示有关多部分 相关
  • Fluent Validation 将 CustomAsync 更改为 MustAsync

    有人可以帮我解决这个问题吗 我正在努力改变CustomAsync 到 MustAsync 但我无法让事情发挥作用 下面是我的自定义方法 RuleFor o gt o MustAsync o gt return CheckIdNumberAl
  • UICollectionview 单元格选择

    我制作了一个图像网格 为了显示其选择 我在选择时为图像绘制了边框 但问题是 当我选择顶部的一些图像并向下滚动图像网格时 底部的其他一些图像似乎也被选择了 下面是我的代码片段 UINib cellNib UINib nibWithNibNam