UITableViewCell 背景隐藏 UI 元素

UITableViewCell background hiding UI Elements

我已经创建了一个自定义的 UITableView Cell,似乎一切正常,只有一个例外。我有一个位于单元格内的 UISwitch(连接到 tableView 加载的它自己的 UITableViewCell class),但它仅在您单击单元格或单元格背景为 clear/transparent 时出现。理想情况下,我有一个白色的单元格背景和背景顶部的开关。

我尝试了一些骇人听闻的东西,例如:

cell.bringSubview(toFront: cell.switch)

cell.switch.isHidden = false

但这显然行不通。

该开关已启用,默认情况下打开。 tableview 和 switch 是从故事板创建的。

层次结构如下所示 - TableView > Cell > Content View > Switch

这里有一段视频可以详细看 - http://quick.as/rpyub8mv

Xcode Storyboard Screenshot

自定义 TableViewCell Class

class SettingsBoolCell: UITableViewCell {

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

}

ViewController 实施

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
}

如果您使用故事板,请确保您的 UISwitch 在正确的视图内,并且在文档大纲中的其他组件下方。

如果您以编程方式在单元格内生成视图,请确保最后使用 addSubView 将 UISwitch 添加到正确的视图。您还可以使用 view.layer.zPosition 属性设置 zPositions。

所以我设置了 -

cell.textLabel.text?

更改 tableView 中单元格的文本。问题是,显然当您使用自定义单元格时,如果没有一些奇怪的行为,您将无法访问默认单元格属性。

感谢大家的帮助!