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

时间:2019-04-01 02:11:18

标签: 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为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
    }
}

这些都是高度的代表,最后一部分具有不同的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
    }
}

1 个答案:

答案 0 :(得分:0)

设法解决了这个问题。

结果是我返回了UIView的{​​{1}},但没有使用viewForHeaderInSection。每次滚动tableView.dequeueReusableHeaderFooterView时,我实际上都会初始化一个新的xib UIView,这将导致它自动调整内容插图,从而导致滚动混乱。

因此,我为tableView类型的节标题视图创建了新的自定义xib,然后将其返回到UITableViewHeaderFooterView

viewForHeaderInSection