更改表格视图行文本和图像颜色

Change tableview row text and image color

我有一个tableView。在那里我创建了自定义单元格。现在我想实现与图像中显示的完全相同的效果。我尝试了很多使用 selectionStyle 和其他东西,但我没有成功。

我想更改背景颜色、字体颜色和图像颜色。我怎样才能做到这一点?这是我的 UITableViewCell 的代码。

    import UIKit

public class BaseTableViewCell : UITableViewCell {
    class var identifier: String { return String.className(self) }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


    }

    public override func awakeFromNib() {
        self.selectionStyle = UITableViewCellSelectionStyle.Gray
    }


    public class func height() -> CGFloat {
        return 40
    }

    public func setData(data: Any?, image : String!, alignment: NSTextAlignment!, font: UIColor) {

        self.textLabel?.font = UIFont(name: "Montserrat-Light", size: 12)
        self.textLabel?.textAlignment = alignment
        self.textLabel?.textColor = font

        if let menuText = data as? String {
            self.textLabel?.text = menuText
        }

        self.imageView?.image = UIImage(named: image)
    }

    override public func setHighlighted(highlighted: Bool, animated: Bool) {
        if highlighted {


        } else {
            self.alpha = 1.0
        }
    }

    // ignore the default handling
    override public func setSelected(selected: Bool, animated: Bool) {

    }

}

谢谢。

添加这个:

override public func setSelected(selected: Bool) {
    super.setSelected(selected)
    // Do your customization here

    // changing text color
    self.textLabel?.textColor = selected ? UIColor.blueColor() : UIColor.blackColor()
}

覆盖 public func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(已选择,动画:真)

     self.textLabel?.textColor = selected ? UIColor.blue : UIColor.black
     self.imageView?.tintColor = selected ? UIColor.blue : UIColor.black

imageView?.image = imageView?.image!.withRenderingMode(.alwaysTemplate) }