切换选择/取消选择UICollectionView Cell的状态 - Swift

时间:2016-05-14 03:41:23

标签: ios swift uicollectionview

首先,我已经坚持了几天,花了一整天阅读并尝试了Stack Overflow上的许多选项,但不是我的成功

我想要完成的事情听起来很简单,并且在Apple文档中看来它应该可行 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/#//apple_ref/occ/intfm/UICollectionViewDelegate/collectionView:shouldHighlightItemAtIndexPath

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

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

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

ViewController -

 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 let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
        cell.toggleSelectedState()
    }
}

细胞 -

    func toggleSelectedState() {
    if selected {
        print("Selected")
        backgroundColor = UIColor.whiteColor()
    } else {
        backgroundColor = UIColor.clearColor()
        print("Deselected")
    }
}

我遇到的问题是当点击已经选择的单元格时没有调用didDeselectItemAtIndexPath,虽然如果我点击另一个单元格它将被调用并选择新单元格...

我已经尝试在shouldSelectItemAtIndexPath&中检查所选状态。 shouldDeselectItemAtIndexPath,我甚至尝试写一个tapGesture来解决这个问题但仍然没有运气......

有什么我想念的吗? 或者有任何已知的工作吗? 任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

使用shouldSelectItemAt并检查集合视图的indexPathsForSelectedItems属性,以确定是否应选择或取消选择该单元格。

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    if let indexPaths = collectionView.indexPathsForSelectedItems, indexPaths.contains(indexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        return false
    }

    return true
}

答案 1 :(得分:0)

也许你可以创建一个与单元格具有相同边界的UIButton(),并识别按钮中的选择。然后在按钮的点击操作中,您可以做一些事情来分析'细胞被选中'。

答案 2 :(得分:0)

您可以将UICollectionView的 allowsMultipleSelection 属性设置为YES(true),然后集合视图不会取消选择上一项。

答案 3 :(得分:0)

我对收集视图进行了这项工作,该视图允许使用轻击手势识别器选择和取消选择多个单元格。

单元格:

class ToggleCollectionViewCell: UICollectionViewCell {
    var didChangeSelection: (Bool) -> Void = { _ in }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTap))
        addGestureRecognizer(tap)
    }

    @objc private func didTap() {
        isSelected.toggle()
        didChangeSelection(isSelected)
    }
}

控制器:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "toggleCell", for: indexPath)

    if let toggleCell = cell as? ToggleCollectionViewCell {
        // do your cell setup...
        lineCell.didChangeSelection = { isSelected in
            guard let self = self else { return }
            // ... handle selection/deselection
        }
    }

    return cell
}
相关问题