首页 > 解决方案 > 实时更新自定义表格部分标题视图内容

问题描述

我有一个自定义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 调用时更改标签文本,但它不会发生。可能是什么问题呢?

编辑:

当我将部分标题滚动到顶部并让它返回时,它运行良好。

标签: iosswiftuitableview

解决方案


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

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

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


推荐阅读