【问题标题】:first tableview headerview disappears after scroll but every other one stays visible滚动后第一个 tableview headerview 消失,但其他每个都保持可见
【发布时间】: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


【解决方案1】:

标题被重复使用。 当您在if 中执行某些操作时,您需要在else 中“重置值”。 好吧,在您的情况下,您是在 else (.alpha = 0) 中执行此操作,但您看到了这个想法。

所以:

if section == 0 || section == 3 || section == 6 {
    header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0)
} else {
    header.textLabel?.alpha = 0
}

应该是:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2013-07-28
    相关资源
    最近更新 更多