如何突出显示所选的UICollectionView单元格?(迅速)

bkw*_*ero 21 ios uicollectionview swift

我有一个UICollectionView,用户可以选择多个单元格.跟踪选择了哪些单元格有点困难,所以我需要一些方法来点击单元格时突出显示/创建边框.

码:

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {

    addToList.append(objectsArray[indexPath.row])

    return true

}
Run Code Online (Sandbox Code Playgroud)

Özg*_*sil 46

您可以在didSelectItemAtIndexPath覆盖事件上使用边框更改,如下面的代码,并在单元格上分配新设置.

Swift 3.x:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    addToList.append(objectsArray[indexPath.row])
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.layer.borderWidth = 2.0
    cell?.layer.borderColor = UIColor.gray.cgColor
}
Run Code Online (Sandbox Code Playgroud)


Daw*_*ong 15

这是我的解决方案,我确信它有效.

我的解决方案包括3种高亮效果,UICollectionCellselectedBackgroundView,cell.contentView.backgroundColor或你自己的specialHighlightedArea.

如何使用?只是继承BaseCollectionViewCell.如果需要,可以在单元格initcollectionView委托方法中进行配置.

如果您不需要高亮效果,只需在UICollectionViewDelegate中找到名为' shouldHighlightItemAtIndexPath '的方法并返回false或只是设置cell.shouldTintBackgroundWhenSelected = false.

extension UIColor {
    convenience init(rgb: Int, alpha: CGFloat = 1.0) {
        self.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgb & 0xFF00) >> 8) / 255.0, blue: CGFloat(rgb & 0xFF) / 255.0, alpha: alpha)
    }
}
/// same with UITableViewCell's selected backgroundColor
private let highlightedColor = UIColor(rgb: 0xD8D8D8) 

class BaseCollectionViewCell: UICollectionViewCell {

    var shouldTintBackgroundWhenSelected = true // You can change default value
    var specialHighlightedArea: UIView?

    // make lightgray background show immediately?on touch?
    // (????????? cell ?????)
    override var isHighlighted: Bool { 
        willSet {
            onSelected(newValue)
        }
    }
    // keep lightGray background from selected until unselected 
    // (???????????)
    override var isSelected: Bool { 
        willSet {
            onSelected(newValue)
        }
    }
    func onSelected(_ newValue: Bool) {
        guard selectedBackgroundView == nil else { return }
        if shouldTintBackgroundWhenSelected {
            contentView.backgroundColor = newValue ? cellHighlightedColor : UIColor.clear
        }
        if let sa = specialHighlightedArea {
            sa.backgroundColor = newValue ? UIColor.black.withAlphaComponent(0.4) : UIColor.clear
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


drf*_*oew 12

SWIFT

Add this code to your UICollectionViewCell subclass:

 override var isSelected: Bool {
    didSet{
        if self.isSelected {
            UIView.animate(withDuration: 0.3) { // for animation effect
                 self.backgroundColor = UIColor(red: 115/255, green: 190/255, blue: 170/255, alpha: 1.0)
            }
        }
        else {
            UIView.animate(withDuration: 0.3) { // for animation effect
                 self.backgroundColor = UIColor(red: 60/255, green: 63/255, blue: 73/255, alpha: 1.0)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

This will set the color of a single selected cell, and remove the selected color from any previous selected cells.


Sun*_*kas 5

使用

collectionView.reloadItemsAtIndexPaths([indexPath])
Run Code Online (Sandbox Code Playgroud)

重新加载当前单元格,或

collectionView.reloadData()
Run Code Online (Sandbox Code Playgroud)

重新加载所有单元格 shouldSelectItemAtIndexPath

然后cellForItemAtIndexPath设置边框或背景色(如果该单元格被标记为选中的话)(对于选中的单元格,最好使用indexPaths,可能需要一个新的数组。


Str*_*ong 5

您可以创建自定义的collcetionViewCell并覆盖:

class MyCell: UICollectionViewCell {

    override var isHighlighted: Bool {
        didSet {
            if self.isHighlighted {
                print("yes")
                // Your customized animation or add a overlay view
            } else {
                print("no")
                // Your customized animation or remove overlay view
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以在UITableViewCell上创建类似的效果,例如突出显示效果。

没有子类化:

如果您不想创建自己的collectionViewCell。您可以使用委托方法:

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath)

func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)

您可以用它做同样的事情。