Swift: UITableView 行项目在隐藏该行时不隐藏

Swift: UITableView row items not hiding when hide that row

我有一个 table 视图 class 和三个单元格 classes,我用它们来填充 3 行 table 视图。screenshot of my table view

我想在切换按钮关闭时隐藏第二行。当我点击切换按钮时,该行会折叠但单元格项目不会隐藏。scrrenshot after tapping toggle button

这是我的代码:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row==0{
            let cell=tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath) as! toggleCell
            return cell

        }
        if indexPath.row==1{
            let cell1=tableView.dequeueReusableCell(withIdentifier: "cellid2", for: indexPath) as! oneButtonCell
            cell1.layer.backgroundColor=UIColor.purple.cgColor
            return cell1
        }
        else{
            let cell1=tableView.dequeueReusableCell(withIdentifier: "cellid1", for: indexPath)
            cell1.layer.backgroundColor=UIColor.green.cgColor
            return cell1
        }
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row==1{
            if istoggleOn==true{
                return 75
            }
            else{
            return 0
            }
        }
        else{
            return 75
        }
    }
}
class CellBase: UITableViewCell{
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupViews(){
        backgroundColor=UIColor.green
    }
}
class toggleCell:CellBase{

    let containerView=UIView()
    let label:UILabel={
        let label=UILabel()
        label.text="Toggle Button"
        label.textColor = .black
        label.font=UIFont.init(name: "HelveticaNeue-Bold", size: 20)
        return label
    }()
    lazy var button:UISwitch={
            let toggle=UISwitch()
            toggle.isOn=true
            toggle.onTintColor=UIColor(red: 59/255, green: 178/255, blue: 250/255, alpha: 1)
            toggle.addTarget(self, action: #selector(handleToggleAction), for: .valueChanged)
            return toggle
        }()
    @objc func handleToggleAction(sender:UISwitch){
         let parent = self.parentViewController as! ViewController

        if sender.isOn{
            print("on")
            parent.istoggleOn=true
            parent.tableView.beginUpdates()
            parent.tableView.endUpdates()
        }
        else{
            print("off")
            parent.istoggleOn=false
            parent.tableView.beginUpdates()
            parent.tableView.endUpdates()
        }
    }
    override func setupViews(){
    addSubview(containerView)
    setupContainerView()

    addConstraintsWithFormat(format: "H:|-20-[v0]-20-|", views: containerView)
    addConstraintsWithFormat(format: "V:|-15-[v0]-15-|", views: containerView)
}
    func setupContainerView(){
}
class oneButtonCell:CellBase{
    let containerView:UIView={
        let view=UIView()
        view.layer.cornerRadius=10
        view.layer.borderColor=UIColor(red: 59/255, green: 178/255, blue: 250/255, alpha: 1).cgColor
        view.layer.borderWidth=2
        return view
    }()
    let label:UILabel={
        let label=UILabel()
        label.text="SomeText"
        label.textColor = .black
        label.font=UIFont.init(name: "HelveticaNeue-Bold", size: 20)
        return label
    }()
    lazy var button:UIButton={
           let button=UIButton()
            button.setTitle("Button", for: .normal)
            button.titleLabel?.font=UIFont.init(name: "HelveticaNeue-Bold", size: 20)
            button.titleLabel?.adjustsFontSizeToFitWidth = true
            button.titleLabel?.minimumScaleFactor = 0.5
            button.setTitleColor(UIColor(red: 59/255, green: 178/255, blue: 250/255, alpha: 1), for: .normal)
            let image=UIImage(named: "arrow")?.withRenderingMode(.alwaysTemplate)
            button.setImage(image, for: .normal)
            button.imageView?.tintColor = .gray
            button.semanticContentAttribute = .forceRightToLeft
            button.addTarget(self, action: #selector(preferenceMenu), for: .touchUpInside)
            return button
        }()
    @objc func preferenceMenu(sender:UIButton){
        print("drop")
    }
    override func setupViews(){
        addSubview(containerView)
        setupContainerView()

        addConstraintsWithFormat(format: "H:|-20-[v0]-20-|", views: containerView)
        addConstraintsWithFormat(format: "V:|-15-[v0(45)]-15-|", views: containerView)
    }
    func setupContainerView(){
}
}

我正在以编程方式进行所有操作。请帮助我如何实现这个

显示单元格的内容是因为您设置了行高 0...但是这种 showing/hiding 行的方法(以及您设置视图的方式)没有改变该单元格的内容

因此,内容仍然显示,因为它延伸到单元格边界以下。

showing/hiding 行还有其他(可以说是更好的)方法,但请坚持使用您的方法...

class CellBase 中的 setupViews() 功能更改为:

func setupViews(){

    // add this line
    self.clipsToBounds = true

    backgroundColor=UIColor.green

}

编辑

由于原始 post 中的代码不完整,我忘了注意每个自定义单元格 class 可能应该在其 [=13] 中包含对 super 的调用=] 功能:

override func setupViews(){
    // adding this line will allow you to put common setup tasks
    // in the setupViews() func in CellBase class
    super.setupViews()

    // do additional setup tasks that are specific to this cell class
    addSubview(containerView)
    setupContainerView()

    addConstraintsWithFormat(format: "H:|-20-[v0]-20-|", views: containerView)
    addConstraintsWithFormat(format: "V:|-15-[v0]-15-|", views: containerView)
}