Riddhi Shah
Riddhi Shah

Reputation: 763

Change tableview row text and image color

I have one tableView. In that I have created custom cell. Now I want to achieve exactly the same effect as shown in image. I tried a lot using selectionStyle and other stuffs, but I am not succeed.

enter image description here

I want to change the backgroundColor, fontcolor and imagecolor. How can I achieve this? This is the code of my 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) {

    }

}

Thank you.

Upvotes: 0

Views: 1469

Answers (2)

user9152579
user9152579

Reputation: 1

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

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

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

Upvotes: -2

CZ54
CZ54

Reputation: 5588

Add this:

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

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

Upvotes: 3

Related Questions