在 didHighlightRowAt 中更改 tableview 单元格中的文本颜色

Change text color in tableview cell in didHighlightRowAt

我有这个代码:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        let cell  = tableView.cellForRow(at: indexPath)
        cell!.contentView.backgroundColor = .blue
        cell?.textLabel?.textColor = UIColor.white
        var cell2 = tableView.cellForRow(at: indexPath) as! DataTableViewCell
       cell2.textLabel.textColor = UIColor.white
    }

背景更改正常,但文本颜色更改无效。

有谁知道为什么以及如何解决这个问题?

我觉得不如写这两行:

cell!.textLabel?.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
cell?.textLabel?.textColor = UIColor.white

只写一行:

cell.textLabel?.highlightedTextColor = .black

适合你!

在您的 DataTableViewCell

中使用此功能
func setHighlighted(_ highlighted: Bool, 
       animated: Bool)

Apple Developer Documentation

尝试以下任一方法:

// set highlighted color in `tableView cellForRowAt indexPath`
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 cell?.textLabel?.highlightedTextColor = UIColor.white
}

或者试试这个

class CustomCell: UITableViewCell {


    // As an alternate of `tableView cellForRowAt indexPath`, label text highlighted color can be set in both these methods of cell - `awakeFromNib` and `prepareForReuse`
    override func awakeFromNib() {
        super.awakeFromNib()
        self.textLabel?.highlightedTextColor = UIColor.white
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.textLabel?.highlightedTextColor = UIColor.white
    }


    // or textColor can be directly set here 
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        self.textLabel?.textColor = UIColor.white
    }
}

好的,这段代码有效:

override func awakeFromNib () {
         super.awakeFromNib ()
         // Initialization code
         self.titleCell? .xtxtColor = UIColor.black
         self.titleCell? .highlightedTextColor = UIColor.white
  }

     override func setSelected (_ selected: Bool, animated: Bool) {
         super.setSelected (selected, animated: animated)

         // Configure the view for the selected state
     }
    
    
     override func prepareForReuse () {
         super.prepareForReuse ()
         self.titleCell? .highlightedTextColor = UIColor.white
     }

非常感谢您的帮助:)

tableView(_:didHighlightRowAt:) 函数是您应用的 Controller 域的一部分,而设置颜色是 View 域的一部分(按照模型—视图—控制器模式)。为了保持良好的职责分离,您可能需要考虑在控制器外部更改颜色,即。 e.在细胞本身。为此,您将创建一个简单的 UITableViewCell 子类,如下所示:

class MyCell: UITableViewCell {
    class var reuseIdentifier: String { return "MyCell" }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        if selected {
            textLabel?.textColor = UIColor.white
        } else {
            textLabel?.textColor = UIColor.black
        }
    }
}

这种方式是单元本身(而不是控制器)处理它自己的图形表示,这是推荐的方式。

cell.textLabel.highlightedTextColor = [UIColor 绿色];