ios 在没有 tableView:titleForHeaderInSection 的情况下更改 UITableView 部分标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1586420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:33:13  来源:igfitidea点击:

Changing UITableView section header without tableView:titleForHeaderInSection

iosuitableview

提问by Scott McCammon

I'm trying to change the header title for a section in a UITableViewwhen a cell from that section is selected. tableView:titleForHeaderInSectionis triggered by the application, so that doesn't help. I can call reloadData, but the performance suffers because the app has to reload all the visible cells. I also tried to use a custom header, but that also results in some performance problems.

我正在尝试在UITableView选择来自该部分的单元格时更改一个部分的标题标题。tableView:titleForHeaderInSection由应用程序触发,因此无济于事。我可以调用reloadData,但性能受到影响,因为应用程序必须重新加载所有可见单元格。我也尝试使用自定义标头,但这也会导致一些性能问题。

Is there any way to get a handle to the UILabelthat the default header view uses and change its text manually?

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

采纳答案by Sixten Otto

There doesn't appear to be any standard API for accessing the system-provided section header view. Have you tried the more targeted reloadSections:withRowAnimationto get UIKit to display the new header text?

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

What kind of performance issues were you seeing with custom section header views? I doubt that the standard one is much more than just a UILabel.

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

回答by Scott McCammon

Calling [tableView endUpdates]may provide the desired results without too much of a performance hit.

调用[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:

回答by Dr. Azrael Tod

with:

和:

[self.tableView headerViewForSection:i]

you could get the view for Section i and then "update" it manually

您可以获取第 i 部分的视图,然后手动“更新”它

this even works if your view is just the auto-generated label, but you will have to resize it yourself. so if you try to:

如果您的视图只是自动生成的标签,这甚至有效,但您必须自己调整它的大小。所以如果你尝试:

[self.tableView headerViewForSection:i].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:i];

you will set the text, but you would not set the label-size. You can get the needed size from NSString to set it yourself:

您将设置文本,但不会设置标签大小。您可以从 NSString 获取所需的大小以自行设置:

[label.text sizeWithFont:label.font];

回答by Ken Ryall

You can set the title for the section header label directly. For example, to set the title of section zero:

您可以直接设置节标题标签的标题。例如,要设置第零部分的标题:

UITableViewHeaderFooterView *sectionZeroHeader = [self.tableView headerViewForSection:0];
NSString *sectionZeroLabel = @"Section Zero";
[sectionZeroHeader.textLabel setText:[sectionZeroLabel uppercaseString]];
[sectionZeroHeader setNeedsLayout];

Be sure and tell the section header view it needs layout, otherwise the new text may be truncated. Also, section labels are usually all uppercase.

一定要告诉部分标题视图它需要布局,否则新文本可能会被截断。此外,节标签通常都是大写的。

回答by David Liu

Since a UITableView does not enqueue and dequeue section header views for reuse, you might as well see if it's feasible to store all the section header views in memory. Note you'll have to create your own section header views with the background and etc., to do this, but it allows you a bit more flexibility and capability.

由于 UITableView 不会将节头视图入队和出队以供重用,因此您不妨看看将所有节头视图存储在内存中是否可行。请注意,您必须使用背景等创建自己的部分标题视图才能做到这一点,但它可以让您获得更多的灵活性和功能。

You could also try tagging the section header views (also requires you to create your own section header views) and just grab them from the tableview as needed.

您还可以尝试标记部分标题视图(还需要您创建自己的部分标题视图),并根据需要从 tableview 中获取它们。

回答by zeroimpl

For completeness, the method we are all looking for is this private API named exactly what you'd expect:

为完整起见,我们都在寻找的方法是这个私有 API,其名称与您所期望的完全相同:

-(void)_reloadSectionHeaderFooters:withRowAnimation:

eg:

例如:

[tableView _reloadSectionHeaderFooters:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]

I do not recommend using this, however.

但是,我不建议使用它。

回答by Alexander Danilov

If you want to update only one section best way is tableView.headerView. If header is not visible it returns nil, so it doesn't load extra headers.

如果您只想更新一个部分,最好的方法是tableView.headerView. 如果标头不可见,则返回 nil,因此它不会加载额外的标头。

if let header = tableView.headerView(forSection: i) {
    header.textLabel!.text = "new title"
    header.setNeedLayout()
}

If you want to update all visible section headers it is better to set header tag as section before view is displayed and enumerate subviews when needed:

如果要更新所有可见的部分标题,最好在显示视图之前将标题标记设置为部分,并在需要时枚举子视图:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tag = section
    // additional customization
}

func udpateVisibleSectionHeaders() {
    for subview in tableView.subviews {
        if let header = subview as? UITableViewHeaderFooterView {
            let section = header.tag
            header.textLabel!.text = "new title"
            header.setNeedsLayout()
        }
    }
}

Don't forget to call setNeedsLayoutor label can be truncated.

不要忘记调用setNeedsLayout或标签可以被截断。

回答by Clay Bridges

This is a WAG, and I can think of lots of reasons why it might not work, but couldn't you iterate through the subviews, and find the one you are looking for? E.g.

这是一个 WAG,我可以想到很多可能无法正常工作的原因,但是您不能遍历子视图并找到您要找的那个吗?例如

for (UIView *v in self.tableView.subviews) {
    // ... is this the one?
}

回答by mangesh

One of the solution would be to manage external array for multiple section headers containing label references and update them externally.

解决方案之一是管理包含标签引用的多个节标题的外部数组,并在外部更新它们。