【问题标题】:swift collection view: how can I change an image view color of one specific cell?swift collection view:如何更改一个特定单元格的图像视图颜色?
【发布时间】:2017-04-08 06:14:57
【问题描述】:

我需要联系我的收藏视图中的一个具体单元格不点击它。例如,我需要更改第二个单元格中图像的颜色并将其变为黑色。你知道有什么解决办法吗?

这是一张图片:

这是我的代码:

import UIKit


class SimplestCollectionViewCell: UICollectionViewCell {

var sImageView: UIImageView!

override func awakeFromNib() {



    sImageView = UIImageView(frame: contentView.frame)
    sImageView.contentMode = .scaleToFill
    sImageView.clipsToBounds = true


    let sImage = UIImage(named: "banana.png")?.withRenderingMode(.alwaysTemplate)
    sImageView.image = sImage
    sImageView.tintColor = UIColor.orange

    contentView.addSubview(sImageView)

    }


}

extension ViewController: UICollectionViewDelegate,     UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 49
}

// we use this method to dequeue the cell and set it up
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sCell", for: indexPath) as! SimplestCollectionViewCell
    cell.awakeFromNib()
    return cell
}

}

更新:建议代码中的编译器错误:

【问题讨论】:

  • 你想要备用背景颜色单元格吗?
  • 不,我只是想将第二个单元格中的香蕉颜色更改为黑色,例如。
  • 为什么不采用一个简单的标志来决定单元格是否需要将背景设置为黑色,在您的 cellForItemAt indexPath 中设置标志值。
  • 谢谢,但我以前从未见过,抱歉。可以给个样品怎么做吗?
  • @Roman 当然我会添加一个相同的答案

标签: swift uicollectionview uicollectionviewcell


【解决方案1】:
import UIKit

class SimplestCollectionViewCell: UICollectionViewCell {

var sImageView: UIImageView!
var shouldCellBeBlack: Bool = false//give any suitable name you like
override func awakeFromNib() {



sImageView = UIImageView(frame: contentView.frame)
sImageView.contentMode = .scaleToFill
sImageView.clipsToBounds = true


let sImage = UIImage(named: "banana.png")?.withRenderingMode(.alwaysTemplate)
sImageView.image = sImage

sImageView.tintColor  = shouldCellBeBlack ? UIColor.black :  UIColor.orange

contentView.addSubview(sImageView)

}


}

extension ViewController: UICollectionViewDelegate,     UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 49
}

// we use this method to dequeue the cell and set it up
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sCell", for: indexPath) as! SimplestCollectionViewCell
    if indexPath.row == 2 //set the value of the cell whose image you want to make black {
      cell.shouldCellBeBlack = true
    }else {
      cell.shouldCellBeBlack = false
    }
    cell.awakeFromNib()
    return cell
}
}

【讨论】:

  • 非常感谢,但我在这一行遇到了快速编译器错误: shouldCellBeBlack ? sImageView.tintColor = UIColor.black : sImageView.tintColor = UIColor.orange 在这个:cell.shouldCellBeBlack = true/false 也许我应该使用更简单的符号
  • true/false 意味着您可以根据自己的方便设置任一值我已经更新了我对您遇到的其他错误的答案。请检查如果您接受此作为正确答案,我将不胜感激,以防它对您有用,干杯!
  • 感谢您的努力和时间,但现在我得到以下结果:所有香蕉都是橙色或黑色。但我需要的是不同的东西:所有香蕉都是橙色的,只有一个(例如,第二个单元格的香蕉)是黑色的。
  • 非常感谢,它有效!我还找到了使用标签的解决方案: cell.sImageView.tag = indexPath.row cell.sImageView.viewWithTag(1)?.tintColor = UIColor.black
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
相关资源
最近更新 更多