3

攻在UICollectionView单UICell时再滚动向下或向上的UICollectionView我看到更多的一个单元格,选择我有一个奇怪的问题。其他未被点击的单元格似乎是在整个UICollectionView布局中随机选择的。我在UICollectionView中有3列单元格和许多行。UICollectionView点击选择多个单元格

在我的代码,我有以下:

- (void)collectionView:(UICollectionView *)myCV didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    LogInfo(@"Item Selected"); 
    // highlight the cell user tapped on 
    UICollectionViewCell *cell = [myCV cellForItemAtIndexPath:indexPath]; 
    [cell.layer setBorderWidth:10.0f]; 
    cell.layer.borderColor = [UIColor colorWithRed: 108/255. green: 166/255. blue: 16/255. alpha:1.0].CGColor; 
    cell.layer.CornerRadius = 10; 

} 

亮点代码只是把边框上的螺纹细胞。

有没有一种方法,以确保只有被点击单元格选择?

回答

2

其因细胞可以被重新使用权?这就是为什么你会得到这个结果。

解决方案是

  1. 保存所选indexpath的地方。

  2. 子类从UICollectionViewCell你的细胞,然后覆盖UICollectionViewCell的prepareForReuse方法,你必须从你所做的(在didSelectItemAtIndexPath法)格式重置为默认值,使细胞准备再次使用。更多关于prepareForReusehere

  3. 在cellForRowItemAtIndexPath再次应用相同的格式到indexpath你第一次保存选定单元格。而已!

但最后,我建议不要直接在这里做任何格式化的东西。尝试理解UICollectionViewLayoutAttributes并使用它来完成这些类型的内容。

+0

Jirune嗨。谢谢你的回复。“将选定的indexpath保存在某处。”你能解释一下我应该保存的位置吗? – motionpotion

+0

Hmm.Just声明一个全局变量NSIndexPath,然后分配选定indexpath该变量,现在根据你刚才申请的打印格式在cellForItemAtIndexPath完蛋了存储索引.. – Jirune

相关问题