簡體   English   中英

不使用tableView更改UITableView節頭:titleForHeaderInSection

[英]Changing UITableView section header without tableView:titleForHeaderInSection

當我選擇該部分的單元格時,我正在嘗試更改UITableView某個部分的標題標題。 tableView:titleForHeaderInSection由應用程序觸發,因此無濟於事。 我可以調用reloadData ,但性能會受到影響,因為應用程序必須重新加載所有可見的單元格。 我也嘗試使用自定義標頭,但這也會導致一些性能問題。

有沒有辦法獲得默認標題視圖使用的UILabel句柄並手動更改其文本?

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

有:

[self.tableView headerViewForSection:i]

您可以獲取Section i的視圖,然后手動“更新”它

如果您的視圖只是自動生成的標簽,這甚至可以工作,但您必須自己調整大小。 所以,如果你試圖:

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

您將設置文本,但不會設置標簽大小。 你可以從NSString獲得所需的大小來自己設置:

[label.text sizeWithFont:label.font];

似乎沒有任何標准API可用於訪問系統提供的節標題視圖。 你有沒有嘗試過更有針對性的reloadSections:withRowAnimation讓UIKit顯示新的標題文本?

您在自定義部分標題視圖中看到了哪些性能問題? 我懷疑標准的不只是UILabel

您可以直接設置節標題標題的標題。 例如,要設置零部分的標題:

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

請確保告訴部分標題視圖它需要布局,否則新文本可能會被截斷。 部分標簽通常都是大寫的。

由於UITableView不會將節標題視圖排入隊列並將其取消以便重用,因此您還可以查看是否可以將所有節標題視圖存儲在內存中。 請注意,您必須使用背景等創建自己的節標題視圖,但這樣可以讓您獲得更多的靈活性和功能。

您還可以嘗試標記節標題視圖(還需要您創建自己的節標題視圖),並根據需要從tableview中獲取它們。

其中一個解決方案是管理包含標簽引用的多個節頭的外部數組,並在外部更新它們。

這是一個WAG,我可以想到很多原因,為什么它可能不起作用,但你不能遍歷子視圖,找到你正在尋找的那個? 例如

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

為了完整起見,我們都在尋找的方法是這個私有API,它的命名完全符合您的期望:

-(void)_reloadSectionHeaderFooters:withRowAnimation:

例如:

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

但是,我不建議使用它。

如果你想只更新一個部分,最好的方法是tableView.headerView 如果標頭不可見,則返回nil,因此不會加載額外的標頭。

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

如果要更新所有可見的節標題,最好在顯示視圖之前將標題標記設置為節,並在需要時枚舉子視圖:

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()
        }
    }
}

不要忘記調用setNeedsLayout或標簽可以被截斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM