ph1lb4 ph1lb4 - 3 months ago 17
Swift Question

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()

Answer

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
        }
    }                
}
Comments