简体   繁体   English

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

[英]UITableView weird scroll behaviour with navigation bar large title, bounce effect at the top auto cut off / jerky when scroll to top

I have a UITableView with multiple sections with the header, collapsable/expandable. 我有一个UITableView具有多个带有标题的部分,可折叠/可扩展。 And also a tableHeaderView with custom UIView . 还有一个带有自定义UIView的tableHeaderView。

Using custom section header UIView and also custom UITableViewCell . 使用自定义节标题UIView和自定义UITableViewCell

When all sections and rows are fully expanded, there is this weird scroll behavior, when I'm scrolling to the top (scroll down), when I reach the very top, the large navigation bar title should follow my scroll down gesture and animate down its way (Bounce effect). 当所有节和行都完全展开时,会出现这种怪异的滚动行为,当我滚动到顶部(向下滚动)时,当到达顶部时,较大的导航栏标题应跟随我的向下滚动手势并向下动画它的方式(弹跳效果)。 However, in this case, the bounce effect did not happen, the moment I scrolled to the top and try to scroll more for bounce effect, the scroll automatically got cut off and the navigation bar automatically becomes a small title. 但是,在这种情况下,并没有发生反弹效果,当我滚动到顶部并尝试进一步滚动以获得反弹效果时,滚动会自动被切断,导航栏会自动变成一个小标题。

Surprisingly, when I collapsed all the rows of the first section, the scroll behavior goes back to normal. 令人惊讶的是,当我折叠第一部分的所有行时,滚动行为又恢复了正常。

Here's a gif to show my screen recording. 这是显示我的屏幕录像的gif文件。

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

I have set the following to my UITableView : 我已经将以下内容设置到我的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

I have also tried: 我也尝试过:

self.tableView.contentInsetAdjustmentBehavior = .never

This indeed solved the weird scrolling behavior, however it causes my section headers and rows to overlap with my tableHeaderView. 这确实解决了怪异的滚动行为,但是这导致我的节标题和行与tableHeaderView重叠。

The way I handle the collapse/expand. 我处理收合/展开的方式。 If the object property isCollapsed is true, I just simply return 0 row for that section: 如果对象属性isCollapsed为true,则只为该部分返回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
    }
}

These are all the height delegates, the last section is having different UITableViewCell , hence the different height. 这些都是高度代表,最后一部分具有不同的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
    }
}

Managed to fix this. 设法解决这个问题。

Turns out I was returning UIView for my viewForHeaderInSection and I wasn't using tableView.dequeueReusableHeaderFooterView . 原来,我正在为viewForHeaderInSection返回UIView ,并且没有使用tableView.dequeueReusableHeaderFooterView I literally init a new xib UIView every time I scroll the tableView which causes it to automatically adjust the content inset, hence, caused the jerky scroll. 我每次滚动tableView都会从字面上初始化一个新的xib UIView ,这会导致它自动调整内容插图,从而导致滚动混乱。

So, I created new custom xib for my section header view with type UITableViewHeaderFooterView , and simply return it at viewForHeaderInSection . 因此,我使用UITableViewHeaderFooterView类型的节标题视图创建了新的自定义xib,并只需将其返回到viewForHeaderInSection

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

    // Configure View...

    return dayHeaderView
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM