【问题标题】:Collection view cell border color Deselect in Swift 3 [closed]Swift 3中的集合视图单元格边框颜色取消选择[关闭]
【发布时间】:2017-08-25 11:17:54
【问题描述】:

使用此代码选择或取消选择集合视图单元格的边框颜色,但它选择了边框颜色但没有取消选择边框颜色。 我用了这么多代码,但没有用。请分享信息

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){

Image2.image = UIImage(named: Images[indexPath.row])

let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 2
cell?.layer.borderColor = UIColor.green.cgColor
collectionView.allowsMultipleSelection = false
//        selectedIndexPath = collectionView.indexPathsForSelectedItems!
}

  private func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: IndexPath) {

let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 2
cell?.layer.borderColor = UIColor.gray.cgColor

}

【问题讨论】:

  • 你的didDeselectItemAtIndexPath函数是private?
  • private func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: IndexPath) 不应调用该方法,因为它不是 Swift 3 版本。
  • 感谢 Piyush Patel 成功了,我用错了方法

标签: ios swift3 uicollectionview


【解决方案1】:
func collectionView(_collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! DateCell

    cell.layer.borderWidth = 2.0
    cell.layer.borderColor = UIColor.gray.cgColor
}
func collectionView(_collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: IndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! DateCell

    cell.layer.borderWidth = 2.0
    cell.layer.borderColor = UIColor.gray.cgColor
}

Try This One 

【讨论】:

    【解决方案2】:

    我使用了您的代码,它运行良好,但只需从 didDeselectItemAt 方法开始时删除 private 关键字

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath)
            cell?.layer.borderWidth = 2
            cell?.layer.borderColor = UIColor.gray.cgColor
        }
    

    【讨论】:

    • 谢谢,是的,我做到了,它的工作原理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多