ph1lb4
ph1lb4

Reputation: 2132

UITableView inside UITableViewCell changes color to white when selected

I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView)

Everything works fine. However there is an issue when I tap on the cell. It gets highlighted, but the nested UITableView changes its color to white which does not look good.

Is there any way to change this? I already set the nested UITableView's backgroundView and its cells to transparent.

EDIT: Selection is disabled for the nested UITableView. The tap occurs on a cell of the outer UITableView, which makes the whole inner UITableView turn white.

EDIT2: Here are some screenshots before and during the tap

Before the tap During the tap

EDIT3: Code of the first UITableView:

In the ViewControler's viewDidLoad

    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.backgroundColor = UIColor.darkBlueColor()
    self.tableView.registerNib(UINib(nibName: "TimelineDay", bundle: nil), forCellReuseIdentifier: TimelineDayTableViewCell.identifier)

In the TimelineDayTableViewCell's awakeFromNib()

    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.backgroundColor = UIColor.darkBlueColor()
    self.tableView.backgroundView = UIView.clearView()
    self.selectedBackgroundView = UIView.viewWithBackgroundColor(UIColor.accentColor())
    self.tableView.registerNib(UINib(nibName: "EventItemCell", bundle: nil), forCellReuseIdentifier: EventItemTableViewCell.identifier)

In the EventItemTableViewCell's awakeFromNib()

    self.backgroundView = UIView.clearView()
    self.selectedBackgroundView = UIView.clearView()

Upvotes: 2

Views: 351

Answers (2)

pedrouan
pedrouan

Reputation: 12910

You may need to subclass your cell:

class YourTableViewCellClass: UITableViewCell
{        
    @IBOutlet weak var yourLabel: UILabel!

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

            self.contentView.backgroundColor = UIColor.red //or what you want
            self.yourLabel.textColor = UIColor.green //or what you want

        } else {

            self.contentView.backgroundColor = UIColor.white //or what you want
            self.yourLabel.textColor = UIColor.green //or what you want
        }
    }                
}

Upvotes: 3

Carlo
Carlo

Reputation: 835

Try with this, good job:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        cellSelected.contentView.backgroundColor = UIColor.blueColor() }

    // if tableView is set in attribute inspector with selection to multiple Selection it should work.

    // Just set it back in deselect 

    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        var cellToDeSelect:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        cellDeselected.contentView.backgroundColor = UIColor.clearColor() }

Upvotes: 1

Related Questions