0
votes

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
}
2
How are you creating the UITableViewCell. Programatically or .xib file. If programatically you need to add it to the ContentView of UITableViewCell. ContentView is subView of UITableViewCell which is used to display your cell content.Sachin Vas
.xib file. Its hierarchy looks like this: Table View > Cell > Content View > UISwitchTyler Rolfe
Here's a video of what I'm talking about - quick.as/rPyUb8mvTyler Rolfe
post a screenshot of your table view layoutMarc-Alexandre Bérubé
Added 'Xcode Storyboard Screenshot' in the original post/question for you to look at and the code. Thanks!Tyler Rolfe

2 Answers

1
votes

If you're using storyboard, make sure that your UISwitch is inside the right view, and that it is under the other components in your document outline.

If you're generating the view inside the cell programmatically, make sure that you add the UISwitch to the right view with addSubView last. You can also set zPositions with view.layer.zPosition attribute.

0
votes

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!