我需要将自定义标题添加到我的表中
我试试这个
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18))
let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
label.text = "TEST TEXT"
label.textColor = UIColor.whiteColor()
self.view.addSubview(view)
return view
}
但这不管用,我什么也没看到
我做错了什么?或者也许还有其他方法?
发布于 2015-08-12 12:39:33
您是否在viewDidLoad中设置了节标题高度?
self.tableView.sectionHeaderHeight = 70
另外,你应该替换
self.view.addSubview(view)
由
view.addSubview(label)
最后,你必须检查你的框架
let view = UIView(frame: CGRect.zeroRect)
以及最终想要的文本颜色,因为它目前看起来是白色的。
发布于 2018-09-05 05:48:33
在UITableView中为swift 4中的部分添加自定义标题视图的最佳工作解决方案是--
#1首先使用方法ViewForHeaderInSection,如下所示:
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 = .systemFont(ofSize: 16)
label.textColor = .yellow
headerView.addSubview(label)
return headerView
}
#2也不要忘记使用heightForHeaderInSection UITableView方法设置header的高度-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
你都准备好了,???
发布于 2017-03-09 11:51:48
如果您使用自定义单元格作为标题,请添加以下内容。
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
}
https://stackoverflow.com/questions/31964941
复制