以编程方式在Swift中隐藏/显示静态节标题

时间:2016-08-29 12:32:42

标签: swift uitableview

我试图在Swift中切换开关时隐藏/显示静态表行和节头。我能够隐藏行但是当我试图隐藏一个部分标题视图时,当我重新打开切换开关时,另一个开始消失。

这就是我的代码:

@IBAction func toggleSound(sender: UISwitch) {

    if(sender.on) {
        self.sectionInVisible = false


    }
    else{
        self.sectionInVisible = true
    }


    let section = 1

    let secPath = NSIndexPath(forRow: 1, inSection: 0)
    self.tableView.cellForRowAtIndexPath(secPath)?.hidden = sectionInVisible
    self.tableView.headerViewForSection(section)?.hidden = sectionInVisible

    let numberOfRows = tableView.numberOfRowsInSection(section)
    for row in 0..<numberOfRows {
        let path = NSIndexPath(forRow: row, inSection: section)
        tableView.cellForRowAtIndexPath(path)?.accessoryType = .None
        tableView.cellForRowAtIndexPath(path)?.hidden = sectionInVisible

    }


    self.tableView.reloadData()


}

我错过了什么?我也尝试过更改高度,但我认为它不能用于静态表。

1 个答案:

答案 0 :(得分:0)

这种事情是错误的:

self.tableView.cellForRowAtIndexPath(secPath)?.hidden = sectionInVisible

删除行的方法是调用deleteRowsAtIndexPaths。类似地,对于部分,还有deleteSections命令。

(您还需要更改基础数据,以便该部分没有这些数据,或者,如果您隐藏整个部分,那么该部分似乎根本不存在。您将,你已经猜到了,用静态表做这件事很麻烦。)

相关问题