小编Brr*_*yce的帖子

在点击时切换 UICollectionView 单元格的选择/取消选择状态 - Swift

所以首先,我已经坚持了几天,花了一整天的时间阅读并尝试了 Stack Overflow 上的许多选项,但没有成功

我试图完成的事情听起来很简单,而且在我看来它应该可以工作的 Apple 文档 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/#//apple_ref/occ /intfm/UICollectionViewDelegate/collectionView:shouldHighlightItemAtIndexPath :

基本上我想要实现的是在点击时切换 UICollectionView Cell 的选定状态。

首先点击 - 将单元格发送到选定状态并将背景颜色更改为白色。

第二次点击 - 将单元格发送到取消选择状态并将背景颜色更改为清除

视图控制器 -

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as? CollectionViewCell {
        cell.cellImage.image = UIImage(named: images[indexPath.row])
        return cell
    } else {
        return CollectionViewCell()
    }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
        cell.toggleSelectedState()
    }
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview swift

1
推荐指数
1
解决办法
4135
查看次数

标签 统计

ios ×1

swift ×1

uicollectionview ×1