如何在didSelect上添加边框到UICollectionViewCell并在选择另一个UICollectionViewCell时删除该边框?

时间:2016-06-12 15:16:31

标签: ios swift uicollectionview uicollectionviewcell

例如,我有10个单元格的UICollectionView。我想为选定的单元格添加边框,稍后在选择另一个单元格时,要删除以前的边框并为新选定的单元格添加边框。

我怎样才能做到这一点?

我试过这个:

var selected = [NSIndexPath]()
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.imageView.image = applyFilter(self.colorCubeFilterFromLUT("\(self.LUTs[indexPath.row])")!, image: self.image!)

    self.selected.append(indexPath)
}

func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
    let cell = self.filtersCollectionView.cellForItemAtIndexPath(indexPath) as! FiltersCollectionViewCell
    cell.imageView.layer.borderWidth = 3.0
    cell.imageView.layer.borderColor = UIColor.brownColor().CGColor
}

func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {

    if self.selected.count > 1 && indexPath == self.selected[self.selected.count - 1] {            
        let cell = self.filtersCollectionView.cellForItemAtIndexPath(indexPath) as! FiltersCollectionViewCell
        cell.imageView.layer.borderWidth = 0.0
        cell.imageView.layer.borderColor = UIColor.clearColor().CGColor
    }
}

但它不起作用。我做错了什么?

4 个答案:

答案 0 :(得分:5)

您可以将选定的indexPath保存到变量中,并在cellForItemAtIndexPath内检查当前indexPath是否等于所选索引路径(您需要重新加载collectionView }每次选中)

var selectedIndexPath: NSIndexPath{
    didSet{
        collectionView.reloadData()
    }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    var borderColor: CGColor! = UIColor.clearColor().CGColor
    var borderWidth: CGFloat = 0

    if indexPath == selectedIndexPath{
        borderColor = UIColor.brownColor().CGColor
        borderWidth = 1 //or whatever you please
    }else{
       borderColor = UIColor.clearColor().CGColor
        borderWidth = 0
    }

    cell.imageView.layer.borderWidth = borderWidth
    cell.imageView.layer.borderColor = borderColor
}

答案 1 :(得分:3)

有一个更简单的解决方案,您已经可以使用UICollectionView提供的功能。此外,由于不必每次选择单个单元格都重新加载collectionView,因此性能会更好。 Apple建议您仅在模型实际更改时才使用reloadData()。

您可以覆盖UICollectionViewCell的属性isSelected,然后只需使用属性观察器自定义其外观即可:

override var isSelected: Bool {
    didSet {
        if isSelected {
            layer.borderWidth = 2
        } else {
            layer.borderWidth = 0
        }
    }
}

请注意,您必须在单元格初始化程序中或您认为方便的地方设置layer.borderColor

PS:如果以后要进行多项选择,则只需启用collectionView中的属性allowsMultipleSelection

答案 2 :(得分:0)

Swift 3版本:

 func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        var borderColor: CGColor! = UIColor.clear.cgColor
        var borderWidth: CGFloat = 0

        if indexPath == selectedIndexPath{
            borderColor = UIColor.brown.cgColor
            borderWidth = 1 //or whatever you please
        }else{
            borderColor = UIColor.clear.cgColor
            borderWidth = 0
        }

        cell.imageView.layer.borderWidth = borderWidth
        cell.imageView.layer.borderColor = borderColor 

   }

答案 3 :(得分:0)

SWIFT 5

   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
         cell.imageView.layer.borderWidth = 2
         cell.imageView.layer.borderColor = UIColor.blue.cgColor
    
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
     
         cell.imageView.layer.borderWidth = 2
         cell.imageView.layer.borderColor = UIColor.clear.cgColor
    }