【问题标题】:Swift: UITableView row items not hiding when hide that rowSwift:隐藏该行时 UITableView 行项不隐藏
【发布时间】:2020-07-01 19:24:59
【问题描述】:

我有一个表格视图类和三个单元格类,用于填充 3 行表格视图。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(){
}
}

我正在以编程方式做所有事情。请帮助我如何实现这一目标

【问题讨论】:

  • 您的numberOfRowsInSection 处理程序在哪里?
  • 使用 tableView 中的 numberOfRows 处理这个问题。如果切换按钮设置为 true,则在 numberOfRows:inSection() 方法中返回 2,否则返回 3,同样根据 @987654326 中的切换状态返回相应的单元格@
  • 我使用这种方法收到此错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。现有节中包含的行数更新后 (2) 必须等于更新前该节中包含的行数 (3),加上或减去从该节中插入或删除的行数(0 插入,0 删除),再加上或减去移入或移出该部分的行数(0 移入,0 移出)。'

标签: ios swift uitableview collapsable


【解决方案1】:

显示单元格的内容是因为您设置了行高0... 但是这种显示/隐藏行的方法(以及您设置视图的方式)不会改变 内容

因此,内容仍在显示,因为它超出了单元格的边界。

还有其他(可以说是更好的)显示/隐藏行的方法,但请坚持您的方法...

class CellBase 中的setupViews() 函数更改为:

func setupViews(){

    // add this line
    self.clipsToBounds = true

    backgroundColor=UIColor.green

}

编辑

由于原始帖子中的代码不完整,我忘了注意每个自定义单元格类可能应该在其setupViews() 函数中包含对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)
}

【讨论】:

  • 当我将它添加到单元基类时它不起作用,但是当我添加 oneButtonCell 类的覆盖 setupViews 函数时它起作用了。感谢您的帮助,您能告诉我显示/隐藏行的其他方法吗?
  • @FazeelaIqbal - 请参阅我的答案的编辑。由于您的原始代码不完整,因此我添加了一些内容(例如将开关添加到 toggleCell 类中的视图)。至于其他显示/隐藏行的方法,这取决于您最终要做什么。对于您的特定(简单)示例,此方法有效-尽管使用闭包/回调比为单元格提供对控制器的引用更好。根据您的需要,稍作搜索会发现其他可能更好的方法。
猜你喜欢
  • 2016-02-13
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多