我有一个 UITableViewController,其表视图具有静态单元格,在故事板中定义。
我的表格视图有两个部分。第一个部分有两个单元,第二个部分有三个单元。第二部分的标题中也有文本。
我想做的是,当用户点击第一部分中的第一个或第二个单元格时,更新第二部分的标题文本。动态地使用动态内容来执行此操作(假设日期和时间显示在他们点击单元格的那一刻)。
我尝试过很多事情,但是viewForHeaderSection
只被调用一次。
我注册了标题部分单元格
tableView.registerClass(TableSectionHeader.self, forHeaderFooterViewReuseIdentifier: "secondSectionCell")
Where TableSectionHeader
很简单:
class TableSectionHeader: UITableViewHeaderFooterView { }
然后我可以动态设置节标题,如下所示:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 1 {
if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("secondSectionCell") {
cell.textLabel?.text = "hello world"
return cell
}
}
return nil
}
我还实现了以下覆盖,因为有些人建议在实现时viewForHeaderInSection
,还需要:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40.0
}
即便如此。viewForHeaderInSection
只被调用一次。
我是否能够按照上述方式动态刷新节标题文本?