实时更新自定义表格部分标题视图内容

时间:2018-07-04 10:48:48

标签: ios swift uitableview

我对自定义tableView标头有问题,即无法实时更新表节内容。

以下是演示我的问题的示例代码:

//SomeViewController.swift
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if let infoHeaderView = InfoHeaderView.viewFromNib as? InfoHeaderView {
       infoHeaderView.update(with: someContent)
       updateMethod = { newContent in
                infoHeaderView.update(with: newContent)
            }
    }
}
...
updateMethod(someContent)
...


//InfoHeaderView.swift
func update(with content: Content) {
    someLabel.text = content.textProperty
}

注意:我想在不重新加载表或单个部分的情况下实现此目的。我只想在自定义表格剖视图中更新单个标签。

我希望在updateMethod调用中更改标签文本,但是不会发生。可能是什么问题?

编辑:

当我将节标题滚动到顶部并让它往下退时,它会很好地工作。

1 个答案:

答案 0 :(得分:1)

使用要更改标题的部分的索引,如下所示:

if let headerView = self.tableView.headerView(forSection: <section number>) as? InfoHeaderView {
    headerView.update(with: content)
}

但是可能要使此方法起作用,您的标题视图应为UITableViewHeaderFooterView子类的类型