我有UICollectionView
有2个部分。我想在用户点击它时选择单元格。
每次用户点击单元格时,我的代码都会正确运行,单元格会变小,并且会在其中显示复选标记(我将imageView
添加为单元格的子视图)。问题是如果我在第一部分点击一个单元格,它会在第二部分中选择另一个单元格。这很奇怪,因为我使用indexPath
。
这是我的代码:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
let cell = collectionView.cellForItemAtIndexPath(indexPath)
let centerCell = cell?.center
if cell!.frame.size.width == cellWidth {
cell?.frame.size.width = (cell?.frame.size.width)!/1.12
cell?.frame.size.height = (cell?.frame.size.height)!/1.12
cell?.center = centerCell!
let imageView = UIImageView()
imageView.image = MaterialIcon.check?.imageWithColor(MaterialColor.white)
imageView.backgroundColor = MaterialColor.blue.accent2
imageView.frame = CGRectMake(1, 1, 20, 20)
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipsToBounds = true
if indexPath.section == 0 {
imageView.tag = indexPath.row+4000
} else {
imageView.tag = indexPath.row+5000
}
print("IMAGEVIEW TAG: ",imageView.tag)
cell?.addSubview(imageView)
}
}
答案 0 :(得分:7)
请务必将collectionView上的multiple selection属性设置为true
或storyboard中的viewDidLoad()
collectionView?.allowsMultipleSelection = true