简体   繁体   English

如何在集合视图中更改视图的颜色

[英]How to change the color of View in collection view

How to change the color of view in collection view when it select and when the select the other view before view came to original color How to set it?当 select 和 select 时,如何更改集合视图中视图的颜色,视图之前的其他视图变为原始颜色 如何设置?

You can use the didSelectItemAtIndexPath method and the didDeselectItemAtIndexPath method from the UICollectionViewDelegate protocol.您可以使用UICollectionViewDelegate协议中的didSelectItemAtIndexPath方法和didDeselectItemAtIndexPath方法。 UICollectionViewDelegate is an Objective-C protocol, indicated by @objc , that allows you to have optional methods like didSelectItemAtIndexPath and didDeselectItemAtIndexPath that you can choose to implement optionally in your delegate. UICollectionViewDelegate是一个 Objective-C 协议,由@objc表示,它允许您拥有可选的方法,例如didSelectItemAtIndexPathdidDeselectItemAtIndexPath ,您可以选择在您的委托中有选择地实现这些方法。

When UICollectionView gets instantiated initially, it goes through all the available methods you've implemented in the instance of UICollectionViewDelegate with responds(to:) , which takes a selector parameter and returns a bool, to see which methods from the UICollectionViewDelegate protocol you've implemented without actually sending a message to them.UICollectionView最初被实例化时,它会遍历您在UICollectionViewDelegate实例中使用responds(to:)实现的所有可用方法,它接受一个选择器参数并返回一个布尔值,以查看您使用了UICollectionViewDelegate协议中的哪些方法在没有实际向他们发送消息的情况下实施。 As it detects the following methods, it remembers them and calls those methods whenever you select or deselect a cell from the collection view:当它检测到以下方法时,它会记住它们并在您 select 或从集合视图中取消选择单元格时调用这些方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    if let indexPaths = collectionView.indexPathsForSelectedItems, let firstIndex = indexPaths.first {
        cell.backgroundColor = firstIndex == indexPath ? .white : .gray
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.backgroundColor = .white
    }
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) else {
        return
    }
    cell.backgroundColor = .gray
}

Alternatively, you can use the collectionView(_:didHighlightItemAt:) method and the collectionView(_:didUnhighlightItemAt:) method.或者,您可以使用collectionView(_:didHighlightItemAt:)方法和collectionView(_:didUnhighlightItemAt:)方法。 The following sample code is from here :以下示例代码来自此处

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.contentView.backgroundColor = #colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1)
    }
}

func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.contentView.backgroundColor = nil
    }
}

You can simply add two methods for changing colour of the view or cell in the collectionView:-您可以简单地添加两种方法来更改 collectionView 中视图或单元格的颜色:-

//When cell got selected //当单元格被选中时

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath){
        cell.backgroundColor = .green
    }
}

//When cell deselected //当单元格取消选择时

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath){
        cell.backgroundColor = .clear
    }
} 

If you have view inside your cell as backgroundView then you can use same functions and just need to replace cell.backgroundView.backgroundColor in place of the cell.backgroundColor.如果您的单元格内部有 backgroundView 视图,那么您可以使用相同的功能,只需要替换 cell.backgroundView.backgroundColor 代替 cell.backgroundColor。 Just remember "backgroundView" is the name of the view you have inside cell.请记住“backgroundView”是您在单元格中拥有的视图的名称。

If you want more information about this then you can follow below attached links:-如果你想了解更多关于这方面的信息,那么你可以点击下面的附加链接:-

1). 1). https://www.tutorialfor.com/questions-296884.html https://www.tutorialfor.com/questions-296884.html

2). 2). https://en.it1352.com/article/fccf358a040b4ddba872d7324602754c.html https://en.it1352.com/article/fccf358a040b4ddba872d7324602754c.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM