【发布时间】:2017-07-14 06:39:09
【问题描述】:
我目前在 UITableView 中使用了一些自定义部分标题视图。视图在加载 UITableView 时出现,但在滚动时消失。我看到了这篇文章,但它似乎已经过时了:tableView section headers disappear SWIFT 这是我的标题视图代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
switch section {
case 0: break
default:
view.backgroundColor = UIColor.clear
let image = UIImageView(image:"Line")
image.frame = CGRect(x: 8, y: 35, width: 340, height: 1)
view.addSubview(image)
let label = UILabel()
let sectionText = self.sectionTitles[section]
label.text = sectionText
label.textColor = UIColor.white
label.font = UIFont(name:"Helvetica Neue" , size: 17)
label.frame = CGRect(x: 10, y: 8, width: 200, height: 20)
view.addSubview(label)
}
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let headerHeight: CGFloat
switch section {
case 0:
// hide the first section header
headerHeight = CGFloat.leastNonzeroMagnitude
default:
headerHeight = 40
}
return headerHeight
}
【问题讨论】:
-
可能不相关,但为什么不为第 0 部分的标题高度返回 0?为什么要返回非常小的、几乎为 0 的值而不是 0?
-
我试过了,真的没什么区别
-
我还建议使用更有用的非零初始帧创建标题视图。
标签: ios swift uitableview