Swift 4 - 如果已选择,则取消选择集合视图单元格

时间:2018-02-05 17:54:16

标签: ios xcode uicollectionview uicollectionviewcell swift4

我有一个集合视图,用户可以在其中选择多个单元格并将索引数组发送到服务器以保存其选择。

除了在创建集合视图时,需要单击所选项目两次以取消选择它们,所以一切正常。

如何解决双击问题?

io.use(function(socket, next) {
  _io = io;
  _sid = socket.id;
  _varName = socket.handshake.query.varName;
  next();
});

1 个答案:

答案 0 :(得分:0)

首先。将其添加到viewDidLoad中。         collectionView?.allowsMultipleSelection = true

然后添加这两个函数。

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell=collectionView.cellForItem(at: indexPath)
    collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
    cell?.backgroundColor = Color.hexStringToUIColor(hex: "#F5A331")
    let lbl = cell?.subviews[1] as! UILabel
    lbl.textColor = Style.cellHighlightedColor
}
 override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    let cell=collectionView.cellForItem(at: indexPath)
    collectionView.deselectItem(at: indexPath, animated: true)

    cell?.backgroundColor = UIColor.white
    let lbl = cell?.subviews[1] as! UILabel
    lbl.textColor = UIColor.darkGray
}
This will do
相关问题