如何向tableview部分标题添加底部边框

Dor*_*hen 4 uitableview ios swift

我正在使用以下函数来确定我的应用程序中的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)
}
Run Code Online (Sandbox Code Playgroud)

以及以下部分确定tableview部分标题高度:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
Run Code Online (Sandbox Code Playgroud)

如何在该部分添加底部边框?

Mik*_*ash 10

你可以创建UIView作为边框,然后添加它:

var border = UIView(frame: CGRectMake(0,40,self.view.bounds.width,1))
border.backgroundColor = UIColor.redColor()
headerView.addSubview(border)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

编辑

CGRectMake来自Swift 3.0不再可用.请CGRect改用