无法在 Expandable table View Cell 中定位 UIElement(Switch)

Unable to position UIElement(Switch) in Expandable table View Cell

参考上图: 我想将 UISwitch 放在 cell4 的 Subcell 中。

我在里面做了这个代码cellForRowAt indexPath

if(indexPath.section == 3) {

    if(indexPath.row == 0) {

        let mySwitch = UISwitch()
        let xpos = cell.frame.width - 60
        let ypos = cell.frame.origin.y + 6
        mySwitch.frame = CGRect(x: xpos, y: ypos, width:50, height: 26)
        mySwitch.setOn(true, animated: false)
        mySwitch.onTintColor = UIColor.init(red: 0.91, green: 0.30, blue: 0.24, alpha: 1.0)
        mySwitch.addTarget(self, action: #selector(switchChanged(sender:)), for: UIControlEvents.valueChanged)
        menuTableView.addSubview(mySwitch)
    }
}

现在发生的事情是: 当 table 视图加载时,最初切换出现在单元格 1 上。 cell4 展开和折叠一次后,它出现在cell4 的子菜单中。如何解决这个位置不正确的问题?

您应该将开关附加到您的单元格而不是 tableView。