Jp4*_*eal 45 uitableview ios swift ios8
我有一个我使用的标头数组
let sectionHeaderTitleArray = ["test1","test2","test3]
Run Code Online (Sandbox Code Playgroud)
他们被展示使用
func tableView[tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
}
Run Code Online (Sandbox Code Playgroud)
现在所有这一切工作正常,但我想修改标题的背景颜色,使它们更加明显(深色)
任何想法,如果我可以在一个简单的行中这样做或我需要使用自定义单元格来创建它
谢谢
更新
//number of sections and names for the table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sectionHeaderTitleArray.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section] as String
}
func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
return self.tableView.backgroundColor = UIColor.lightGrayColor()
}
Run Code Online (Sandbox Code Playgroud)
Jov*_*vic 77
如果您只使用titleForHeaderInSection:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}
Run Code Online (Sandbox Code Playgroud)
Jac*_*k C 32
而不是使用
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String?
Run Code Online (Sandbox Code Playgroud)
数据源方法,你可以使用
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Run Code Online (Sandbox Code Playgroud)
委托方法,只需根据需要自定义UIView
返回.
例如设置的文本UILabel
textLabel
到您想要的值,backgroundColor
所需UIColor
.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let returnedView = UIView(frame: CGRectMake(x, y, width, height)) //set these values as necessary
returnedView.backgroundColor = UIColor.lightGrayColor()
let label = UILabel(frame: CGRectMake(labelX, labelY, labelWidth, labelHeight))
label.text = self.sectionHeaderTitleArray[section]
returnedView.addSubview(label)
return returnedView
}
Run Code Online (Sandbox Code Playgroud)
AMA*_*N77 16
你必须保持两者
titleForHeaderInSection
和
viewForHeaderInSection
这是Swift 3中的一些工作代码
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderTitleArray[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
returnedView.backgroundColor = .lightGray
let label = UILabel(frame: CGRect(x: 10, y: 7, width: view.frame.size.width, height: 25))
label.text = self.sectionHeaderTitleArray[section]
label.textColor = .black
returnedView.addSubview(label)
return returnedView
}
Run Code Online (Sandbox Code Playgroud)
小智 13
斯威夫特5 iOS 13:
//Remove 'override' if you don't override from UITableviewController
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerView = view as? UITableViewHeaderFooterView else { return }
headerView.tintColor = .clear //use any color you want here .red, .black etc
}
Run Code Online (Sandbox Code Playgroud)
小智 10
SWIFT 4
超级简单,通过设置标题的视图,contentView背景颜色..
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId") as! CustomHeader
header.contentView.backgroundColor = AnyColor
return header
}
Run Code Online (Sandbox Code Playgroud)
Ash*_*shu 10
斯威夫特4.X
这是经过测试的工作代码。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Total Count (41)"
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.lightGray
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42719 次 |
最近记录: |