簡體   English   中英

如何在UITableView中設置特定的節標題[Swift]

[英]How to set specific section header in UITableView [Swift]

我想以編程方式將節標題添加到UITableView的節中。

我正在嘗試使用此代碼,但是它將標頭添加到所有部分。 如何僅設置第一部分的標題?

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "My Section header"
}

像這樣

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0{ // <- apply condition where you wanted to show header or not.
        return "My Section header"
    }
    return nil
}

使用開關盒可以編寫更好的代碼

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
     switch section {
     case 0: return "Section 0"
     case 1: return "Section 1"
     default: return nil
     }
}

您只需要檢查部分等於一

 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return (section == 1) ? "Title Header" : nil
}

暫無
暫無

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

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