我目前在 UITableView 中使用了一些自定义节标题视图。加载 UITableView 时视图会出现,但滚动时会消失。我看过这篇文章,但它似乎已经过时了:tableView 部分标题消失 SWIFT https://stackoverflow.com/questions/30149551/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
}
标头仅在以下情况下保持固定:UITableView样式表的属性设置为UITableViewStylePlain。如果将其设置为 UITableView Style Grouped,标题将与单元格一起向上滚动。
因此,如果你想修复会粘在顶部的标题,那么你也需要设置 UITableView Style UITableView StylePlain
如果您想向上滚动单元格,则需要设置UITableView样式 to UITableView样式分组
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)