ios - 如何将底部边框添加到 TableView 部分标题

标签 ios uitableview 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)
}

和以下部分确定 TableView 部分标题高度:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

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

最佳答案

你可以创建 UIView 来充当边框,然后添加它:

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

headerView 是您实际创建的自定义 View

编辑

 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

编辑

CGRectMake 在 Swift 3.0 中不再可用。使用 CGRect 代替

关于ios - 如何将底部边框添加到 TableView 部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460367/

相关文章:

ios - 正确检查 NSDictionary 结构

ios - 我很难理解苹果 block 编程的这一部分

ios - 没有堆叠的多个 UIView 数组

swift: prepareForSegue indexPathForSelectedRow

ios - 为数据库设置加密 key (Sybase 无线平台)

ios - 选择行时奇怪的 tableView 行/部分动画

ios - 表格单元格横向模式下的 Youtube 播放器

swift - 为什么要在函数中使用参数?

ios - tableview visibleCells 导致崩溃

swift - 如何重构我的代码以避免重复