[英]Add UIView Above All Section in UITableView Swift
您正在尋找的是節標題。 您可以通過實現UITableViewDataSource
方法輕松配置自己的自定義視圖
例如:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
// configure view, note the method gives you the section to help this process
return headerView
}
您可能還想實現此功能
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50 // or whatever it is
}
請記住,默認情況下標頭是粘性的
在過去,我已經很容易做到這一點:
let dummyViewHeight = CGFloat(49) // height of header (set in storyboard)
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
self.tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)
基本上,這只是使表格標題“粘”在表格頂部的此插圖上。 (除非您確實希望它懸停在頂部部分)
給定OP認為這是“不令人滿意的”,那么當我說唯一的其他合理選擇(如果您不希望使用節標題或頁腳視圖)時,我想99%的信心是然后創建一個新的UITableViewCell
子類。 然后,該新子類將出現在每個部分的頂部。
因此,在cellForIndexPath:
方法中,您將檢查indexPath.row == 0
,如果是,則使具有不同標識符的單元出隊。 這樣,標頭就變成了以不同方式構建的單元。
如果您以編程方式進行操作,則只需確保注冊該單元格( tableView.register(MyNewHeaderCellSubclass.self, forCellReuseIdentifier: "MyNewHeaderCellSubclass")
),否則,請轉到情節tableView.register(MyNewHeaderCellSubclass.self, forCellReuseIdentifier: "MyNewHeaderCellSubclass")
上的表格視圖並切換兩種類型的動態單元格的選項類。
但是,節標題正是此用例的用途。 我不能想到任何其他解決方案,而不僅僅是完全放棄UITableView
,我認為這將是一個大錯誤。 否則,祝您好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.