ios - 带有导航栏大标题的 UITableView 奇怪的滚动行为,滚动到顶部时顶部的弹跳效果自动切断/生涩

标签 ios swift uitableview

我有一个 UITableView,其中包含多个带有标题的部分,可折叠/展开。还有一个带有自定义 UIView 的 tableHeaderView。

使用自定义部分标题 UIView 和自定义 UITableViewCell

当所有部分和行都完全展开时,会出现这种奇怪的滚动行为,当我滚动到顶部(向下滚动)时,当我到达最顶部时,大导航栏标题应该遵循我向下滚动的手势并沿途制作动画(弹跳效果)。然而,在这种情况下,反弹效果并没有发生,当我滚动到顶部并尝试滚动更多以获得反弹效果时,滚动自动被切断并且导航栏自动变成一个小标题。

令人惊讶的是,当我折叠第一部分的所有行时,滚动行为恢复正常。

这是一个 gif 来显示我的屏幕录制。

https://i.imgur.com/WwXmpmZ.gifv

我已将以下内容设置到我的 UITableView:

self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 15, right: 0)
self.tableView.estimatedRowHeight = 0 
self.tableView.estimatedSectionHeaderHeight = 0
self.tableView.estimatedSectionFooterHeight = 0

我也试过:

self.tableView.contentInsetAdjustmentBehavior = .never

这确实解决了奇怪的滚动行为,但是它导致我的部分标题和行与我的 tableHeaderView 重叠。

我处理折叠/展开的方式。如果对象属性 isCollapsed 为真,我只是简单地为该部分返回 0 行:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == self.scheduleCount {
        //Last section
        return self.dataSource.count
    }

    guard let schedule = self.itineraryDataSource?.schedule[section] else { return 0 }

    if schedule.isCollapsed {
        return 0
    } else {
        return schedule.items.count
    }
}

这些都是高度委托(delegate),最后一部分具有不同的 UITableViewCell,因此具有不同的高度。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    if section == self.scheduleCount {
        //Last section
        return 40
    }

    return 64
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == self.scheduleCount {
        //Last section
        return 112
    } else {
        return 76
    }
}

最佳答案

设法解决这个问题。

原来我正在为我的 viewForHeaderInSection 返回 UIView 而我没有使用 tableView.dequeueReusableHeaderFooterView。每次滚动 tableView 时,我都会初始化一个新的 xib UIView ,这会导致它自动调整内容插入,因此导致滚动不稳定。

因此,我为我的部分标题 View 创建了新的自定义 xib,类型为 UITableViewHeaderFooterView,并简单地在 viewForHeaderInSection 返回它。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let dayHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ItineraryDayHeaderView") as? ItineraryDayHeaderView

    // Configure View...

    return dayHeaderView
}

关于ios - 带有导航栏大标题的 UITableView 奇怪的滚动行为,滚动到顶部时顶部的弹跳效果自动切断/生涩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55447298/

相关文章:

ios - 动态链接对象没有 url

ios - 登录模态视图未显示在 Split View上

ios - 在 UITableviewcell 的 ios 11 布局 subview subview 中找不到 UITableViewCellDeleteConfirmationView?

ios - 删除数组和 UITableView 中的最后一项

ios - UITableView 部分重复数据

ios - prepareForSegue :sender: gets wrong sender

ios - 在 iOS 上安装多种风格时,Firebase 动态链接无法打开正确的应用程序

ios - 当 SceneKit 动画停止时,SpriteKit 的动画覆盖在 SceneKit 上停止

Swift:WKWebView 需要身份验证。

json - 将 JSON 转换为属性选项数组