不使用tableView更改UITableView节头:titleForHeaderInSection

17 uitableview ios

我正在尝试更改某个部分的标题标题,UITableView当选择该部分的单元格时.tableView:titleForHeaderInSection是由应用程序触发,所以这没有帮助.我可以打电话reloadData,但性能受到影响,因为应用程序必须重新加载所有可见的单元格.我也尝试使用自定义标头,但这也会导致一些性能问题.

有没有办法获得UILabel默认标题视图使用和手动更改其文本的句柄?

谢谢!

Sco*_*mon 24

呼叫[tableView endUpdates]可以提供期望的结果而不会有太多的性能损失.

[self.tableView beginUpdates];
[self.tableView endUpdates];

// forces the tableView to ask its delegate/datasource the following:
//   numberOfSectionsInTableView:
//   tableView:titleForHeaderInSection:
//   tableView:titleForFooterInSection:
//   tableView:viewForHeaderInSection:
//   tableView:viewForFooterInSection:
//   tableView:heightForHeaderInSection:
//   tableView:heightForFooterInSection:
//   tableView:numberOfRowsInSection:
Run Code Online (Sandbox Code Playgroud)

  • 在 iOS10/11 (swift) 我注意到 `tableView:viewForHeaderInSection:` 和 `tableView:viewForFooterInSection:` 没有被调用所以你需要自己改变视图 (3认同)

Dr.*_*Tod 16

有:

[self.tableView headerViewForSection:i]
Run Code Online (Sandbox Code Playgroud)

您可以获取Section i的视图,然后手动"更新"它

如果您的视图只是自动生成的标签,这甚至可以工作,但您必须自己调整大小.所以,如果你试图:

[self.tableView headerViewForSection:i].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:i];
Run Code Online (Sandbox Code Playgroud)

您将设置文本,但不会设置标签大小.你可以从NSString获得所需的大小来自己设置:

[label.text sizeWithFont:label.font];
Run Code Online (Sandbox Code Playgroud)

  • [self.tableView headerViewForSection:i]总是给出nil值 (2认同)
  • 如果headerViewForSection返回nil,则表示标题视图不可见。适用于动态表。 (2认同)

Six*_*tto 14

似乎没有任何标准API可用于访问系统提供的节标题视图.您是否尝试过更有针对性reloadSections:withRowAnimation的让UIKit显示新的标题文本?

您在自定义部分标题视图中看到了哪些性能问题?我怀疑标准的不仅仅是一个UILabel.