Ale*_*y K 31 objective-c uitableview ios swift
我需要在我的表中添加自定义标题
我试试这个
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
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我在桌子上看不到任何东西
我究竟做错了什么 ?或许还有另外一种方法?
Moh*_* G. 29
在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
}
Run Code Online (Sandbox Code Playgroud)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
Run Code Online (Sandbox Code Playgroud)
Tan*_* G. 22
您是否在viewDidLoad中设置了节标题高度?
self.tableView.sectionHeaderHeight = 70
Run Code Online (Sandbox Code Playgroud)
另外你应该更换
self.view.addSubview(view)
Run Code Online (Sandbox Code Playgroud)
通过
view.addSubview(label)
Run Code Online (Sandbox Code Playgroud)
最后你必须检查你的帧
let view = UIView(frame: CGRect.zeroRect)
Run Code Online (Sandbox Code Playgroud)
并最终得到所需的文字颜色,因为它似乎是白色的白色.
Gay*_*anK 16
如果您愿意将自定义表头用作表头,请尝试以下操作....
更新了swift 3.0
步骤1
为自定义标题创建UITableViewHeaderFooterView ..
import UIKit
class MapTableHeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var testView: UIView!
}
Run Code Online (Sandbox Code Playgroud)
第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
}
}
Run Code Online (Sandbox Code Playgroud)
A.G*_*A.G 14
如果您使用自定义单元格作为标题,请添加以下内容.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
headerView.addSubview(headerCell)
return headerView
}
Run Code Online (Sandbox Code Playgroud)
如果您想要简单的视图,请添加以下内容.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:UIView = UIView()
return headerView
}
Run Code Online (Sandbox Code Playgroud)
Bas*_*lam 12
这对我有用-Swift 3
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
return headerCell
}
Run Code Online (Sandbox Code Playgroud)
添加label到subview自定义view,不需要self.view.addSubview(view),因为viewForHeaderInSection返回UIView
view.addSubview(label)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80575 次 |
| 最近记录: |