Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift集合视图:如何更改特定单元格的图像视图颜色?_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

swift集合视图:如何更改特定单元格的图像视图颜色?

swift集合视图:如何更改特定单元格的图像视图颜色?,swift,uicollectionview,uicollectionviewcell,Swift,Uicollectionview,Uicollectionviewcell,我需要在我的收藏视图中联系一个具体单元,而不点击它。例如,我需要更改第二个单元格中图像的颜色并使其变为黑色。你知道有什么解决办法吗 这是一张照片: 这是我的密码: import UIKit class SimplestCollectionViewCell: UICollectionViewCell { var sImageView: UIImageView! override func awakeFromNib() { sImageView = UIImageView(f

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

这是一张照片:

这是我的密码:

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我肯定会为sameThank添加一个答案,但我在这行中遇到了一些快速编译器错误: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
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
}
}