滚动后第一个 tableview headerview 消失,但其他所有的都保持可见

问题描述

所以我的目标是让我的第一个 tableview headerview 在向下滚动后保持可见。所以我有一个 inset grouped 样式的表格视图,以便标题随着单元格向上移动,当视图加载时,第一个标题标签是可见的,如下所示:

Header on view loaded

这太棒了。我喜欢它。现在的问题是当我开始在 tableview 中向下滚动并且单元格从视图中消失时,当我向上滚动时,单元格标题消失了:

Missing cell header

关于这个的奇怪之处在于,有三个带有单元格的部分,当视图加载并要求用户向下滚动时,最后一部分不在视图中,但是当我向下滚动到它时,向上滚动到它不在的位置可见,再次向下滚动,标题视图仍然存在,我不明白。为什么第一个部分标题与最后一个部分标题的行为不同?

这是我的标题配置代码

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()
    
    
}

有什么建议吗?

解决方法

标题被重用。 当您在 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
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...