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

标签 swift uitableview

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

这就是我的代码:

@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()


}

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

最佳答案

这种事情是错误的:

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

删除行的方法是调用deleteRowsAtIndexPaths。与此类似,对于部分,有一个 deleteSections 命令。

(您还需要更改基础数据,以便该部分没有这些数据,或者,如果您隐藏整个部分,则该部分看起来根本不存在。您将像您一样已经猜到了,使用静态表很难做到这一点。)

关于swift - 以编程方式在 Swift 中隐藏/显示静态节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206453/

相关文章:

ios - Swift 2.0 如何检测用户握住而不是仅仅点击

ios - 如何像UITableView的tableHeaderView一样在UICollectionView中添加HeaderView

ios - 用户在 OpenFire 中在线显示,即使应用程序处于后台或处于终止状态

swift - nil 不能转换为 hashable。

iphone - 导航 Controller 上的编辑按钮不进入编辑模式

ios - 我如何使用解码器在 swift 4 中解析 tableview 中的 json 数组

iphone - 为什么会出现过度释放呢? UINavigationController/UITableview

ios - 在解析中用用户的好友列表更新 Tableview

ios - 如何使用 ASWebAuthenticationSession 获取授权码?

ios - 如何从后台任务添加和重绘 CALayer?