我使用以下函数来确定应用中的tableview部分标题:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
var title: UILabel = UILabel()
title.text = "something"
// Add a bottomBorder
var border = UIView(frame: CGRectMake(0,0,self.view.bounds.width,1))
border.backgroundColor = UIColor.redColor()
title.addSubview(border)
}
以及以下部分确定tableview部分标题高度:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
如何在章节中添加底部边框?
答案 0 :(得分:9)
您可以创建UIView作为边框,然后添加它:
var border = UIView(frame: CGRectMake(0,40,self.view.bounds.width,1))
border.backgroundColor = UIColor.redColor()
headerView.addSubview(border)
headerView
是您实际创建的自定义视图
修改强>
var headerView = UIView(frame: CGRectMake(0,0,self.view.bounds.width,40))
var title = UILabel(frame: CGRectMake(0, 0, 200, 21))
title.text = "something"
// Add a bottomBorder
var border = UIView(frame: CGRectMake(0,39,self.view.bounds.width,1))
border.backgroundColor = UIColor.redColor()
headerView.addSubview(border)
headerView.addSubview(title)
return headerView
修改强>
Swift 3.0不再提供 CGRectMake
。请改用CGRect
答案 1 :(得分:1)
Swift 3
这对我有用。在viewForHeaderInSection中:
let headerView = UIView()
let borderTop = UIView(frame: CGRect(x:0, y:0, width: tableView.bounds.size.width, height: 1.0))
borderTop.backgroundColor = UIColor.self.init(red: 5/255, green: 16/255, blue: 28/255, alpha: 1.0)
headerView.addSubview(borderTop)
let borderBottom = UIView(frame: CGRect(x:0, y:40, width: tableView.bounds.size.width, height: 1.0))
borderBottom.backgroundColor = UIColor.self.init(red: 5/255, green: 16/255, blue: 28/255, alpha: 1.0)
headerView.addSubview(borderBottom)
您可以根据标题高调整顶部和底部位置。对于我来说,y = 0表示顶部,x = 40表示底部边框。希望这会有所帮助。祝你好运。
如果您想自定义标题的其余部分:
let headerLabel = UILabel(frame: CGRect(x: 15, y: 9, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Trebuchet MS", size: 19)
headerLabel.textColor = UIColor.self.init(red: 254/255, green: 170/255, blue: 25/255, alpha: 1.0)
headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)