滚动时 UITableView 部分标题视图消失 [英] UITableView section header views disappear when scrolling
问题描述
我目前在 UITableView 中使用了一些自定义部分标题视图.视图在加载 UITableView 时出现,但在滚动时消失.我看到了这篇文章,但它似乎已经过时了:tableView 部分标题消失 SWIFT这是我的标题视图代码:
I currently have some custom section header views used in a UITableView. The views appear when the UITableView is loaded but disappears when scrolling. I saw this post but it seems outdated: tableView section headers disappear SWIFT Here is my code for the header view:
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
}
推荐答案
只有当表的 UITableViewStyle 属性设置为 UITableViewStylePlain 时,标题才保持固定.如果您将其设置为 UITableViewStyleGrouped,则标题将随单元格一起向上滚动.
The headers only remain fixed when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
因此,如果您想修复会粘在顶部的标题,则需要将 UITableViewStyle 设置为 UITableViewStylePlain
So if you want to fix headers that will stick on the top than you need to set UITableViewStyle to UITableViewStylePlain
如果要向上滚动单元格,则需要将 UITableViewStyle 设置为 UITableViewStyleGrouped
if you want to scroll up with the cells than you need to set UITableViewStyle to UITableViewStyleGrouped
这篇关于滚动时 UITableView 部分标题视图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!