我需要在表格中添加自定义标题
我试试这个
D
但这不起作用,我在桌子上看不到任何内容
我做错了什么?或者还有其他方法?
答案 0 :(得分:20)
您是否在viewDidLoad中设置了部分标题高度?
self.tableView.sectionHeaderHeight = 70
另外你应该替换
self.view.addSubview(view)
通过
view.addSubview(label)
最后你必须检查你的框架
let view = UIView(frame: CGRect.zeroRect)
并最终获得所需的文字颜色,因为它似乎是白色的白色。
答案 1 :(得分:14)
如果您愿意将自定义表头用作表头,请尝试以下操作....
更新了swift 3.0
第1步
为自定义标题创建UITableViewHeaderFooterView ..
import UIKit
class MapTableHeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var testView: UIView!
}
第2步
将自定义标头添加到UITableView
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
//register the header view
let nibName = UINib(nibName: "CustomHeaderView", bundle: nil)
self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView")
}
extension BranchViewController : UITableViewDelegate{
}
extension BranchViewController : UITableViewDataSource{
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView" ) as! MapTableHeaderView
return headerView
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
// retuen no of rows in sections
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// retuen your custom cells
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func numberOfSections(in tableView: UITableView) -> Int {
// retuen no of sections
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// retuen height of row
}
}
答案 2 :(得分:14)
在UITableView中为swift 4中的部分添加自定义标题视图的最佳工作解决方案是-
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
let label = UILabel()
label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
label.text = "Notification Times"
label.font = UIFont().futuraPTMediumFont(16) // my custom font
label.textColor = UIColor.charcolBlackColour() // my custom colour
headerView.addSubview(label)
return headerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
答案 3 :(得分:10)
如果您使用自定义单元格作为标题,请添加以下内容。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
headerView.addSubview(headerCell)
return headerView
}
如果您想拥有简单的视图,请添加以下内容。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:UIView = UIView()
return headerView
}
答案 4 :(得分:7)
将label
添加到自定义subview
的{{1}},不需要view
,因为self.view.addSubview(view)
会返回viewForHeaderInSection
UIView
答案 5 :(得分:5)
这对我有用 - Swift 3
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
return headerCell
}
答案 6 :(得分:1)
我在Swift 5中遇到了一些问题。使用此功能时,我与标头单元格的对齐方式错误:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
return headerCell
}
显示的单元格视图对齐不良,并且显示了表格视图的顶部。因此,我必须像这样进行一些调整:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 90))
let headerCell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")
headerCell?.frame = headerView.bounds
headerView.addSubview(headerCell!)
return headerView
}
我在Swift 5和Xcode 12.0.1中遇到了这个问题,我不知道这对我来说是一个问题还是一个错误。希望对您有帮助!我失去了一个早晨...