【发布时间】:2021-06-26 17:10:03
【问题描述】:
所以我的目标是让我的第一个 tableview headerview 在向下滚动后保持可见。所以我有一个带有inset grouped 样式的表格视图,这样标题会随着单元格向上移动,当视图加载时,第一个标题标签是可见的,如下所示:
这很棒。我喜欢它。现在的问题是当我开始在表格视图中向下滚动并且单元格从视图中消失时,当我向上滚动时单元格标题消失了:
关于此的奇怪之处在于,有三个部分带有单元格,当视图加载并要求用户向下滚动时,最后一部分不在视图中,但是当我向下滚动到它时,又向上滚动到它不是的地方可见,然后再次向下滚动,标题视图仍然存在,我不明白。为什么第一节标题与最后一节标题的行为不同?
这是我的标题配置代码:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Account Settings"
} else if section == 3 {
return "Support & Info"
} else if section == 6 {
return "Notification Settings"
} else {
return "label"
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
if section == 0 || section == 3 || section == 6 {
header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0)
} else {
header.textLabel?.alpha = 0
}
header.textLabel?.textAlignment = NSTextAlignment.left
header.backgroundConfiguration = .clear()
}
有什么建议吗?
【问题讨论】:
-
视图被重用。由于您在
else案例中执行header.textLabel?.alpha = 0,因此在if案例中执行相反的操作:header.textLabel?.alpha = 1。 -
没关系,我明白你的意思,它现在很好用,谢谢。 @Larme
-
我的意思是
if section == 0 || section == 3 || section == 6 { header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0); header.textLabel?.alpha = 1 } else { header.textLabel?.alpha = 0 }
标签: ios swift uitableview uitableviewsectionheader