【问题标题】:How to change the color of View in collection view如何在集合视图中更改视图的颜色
【发布时间】:2020-11-14 12:27:58
【问题描述】:

collection view 选择时如何改变view 的颜色,以及在view 之前选择其他view 时如何改变颜色如何设置?

【问题讨论】:

标签: ios swift iphone xcode


【解决方案1】:

您可以使用来自UICollectionViewDelegate 协议的didSelectItemAtIndexPath 方法和didDeselectItemAtIndexPath 方法。 UICollectionViewDelegate 是一个 Objective-C 协议,由 @objc 表示,它允许您拥有可选方法,例如 didSelectItemAtIndexPathdidDeselectItemAtIndexPath,您可以选择在您的委托中选择实现这些方法。

UICollectionView 最初被实例化时,它会遍历您在UICollectionViewDelegate 实例中实现的所有可用方法,responds(to:),它接受一个选择器参数并返回一个布尔值,以查看哪些方法来自UICollectionViewDelegate 协议您已经实现,但实际上并未向他们发送消息。当它检测到以下方法时,它会记住它们并在您从集合视图中选择或取消选择单元格时调用这些方法:

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
}

或者,您可以使用collectionView(_:didHighlightItemAt:) 方法和collectionView(_:didUnhighlightItemAt:) 方法。以下示例代码来自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
    }
}

【讨论】:

    【解决方案2】:

    您可以简单地添加两种方法来更改 collectionView 中视图或单元格的颜色:-

    //当单元格被选中时

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

    //取消选中单元格时

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

    如果您的单元格内有视图作为 backgroundView,那么您可以使用相同的功能,只需要替换 cell.backgroundView.backgroundColor 来代替 cell.backgroundColor。请记住“backgroundView”是您在单元格内的视图的名称。

    如果您想了解更多信息,可以点击以下附加链接:-

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

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

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多