Search code examples
iosswiftuitableviewswift3uiswitch

UITableViewCell background hiding UI Elements


I've created a custom UITableView Cell and everything seems to be working with one exception. I have a UISwitch that is inside the cell (hooked up to it's own UITableViewCell class that the tableView loads) but it only appears when you click on the cell or the cells background is clear/transparent. Ideally I have a white background for the cell and the switch on top of the background.

I've tried some hacky stuff like:

cell.bringSubview(toFront: cell.switch)

and

cell.switch.isHidden = false

But that obviously didn't work.

The switch is enabled and ON by default. The tableview and switch is created from storyboards.

The hierarchy looks like this - TableView > Cell > Content View > Switch

Here's a video to see in detail - http://quick.as/rpyub8mv

Xcode Storyboard Screenshot

Custom TableViewCell Class

class SettingsBoolCell: UITableViewCell {

@IBAction func switchAction(_ sender: UISwitch) {
}
@IBOutlet weak var switchOutlet: UISwitch!

}

ViewController Implementation

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "settingsSectionOne", for: indexPath) as! SettingsBoolCell
    switch indexPath.section {
    case 0: cell.textLabel?.text = titles[0]
    case 1: cell.textLabel?.text = titles[1]
    case 2: cell.textLabel?.text = titles[2]
    default: ()
    }
    return cell
}

Solution

  • So I was setting -

    cell.textLabel.text?
    

    to change the text of the cells inside the tableView. The problem is, apparently when you are using a custom cell, you can't access the default cell properties without some funky behavior.

    Thanks everyone for your help!