OGeek|极客世界-中国程序员成长平台

标题: ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:18
标题: ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动

好的,我有简单的 UIScrollView 和两个 subview :UIViewUITableView 设置为垂直模式。它们都有自己的高度限制:

我是这样设置的:

tableViewHeightConstraint.constant = CGFloat(height) //height is calculated based on number of cells multiplied 130

calendarViewHeightConstraint.constant = UIApplication.shared.keyWindow!.rootViewController!.traitCollection.isIpad ? 630 : 470

但有时当我滚动到底部,然后在表格 View 中添加一个新单元格并重新计算高度时, ScrollView 的滚动似乎被锁定,我可以弹跳但滚动消失,我只能弹跳,虽然我知道我可以滚动到屏幕上当前 View 上方或下方的内容。可能是什么原因?

我真的必须这样做。不可能将我的 View 放在表格 View 标题 View 中。请让我们离开这个话题;)

这是我重新计算高度的地方:

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {

        tableView.endUpdates()
        updateView()
        updateTableViewHeight()
}

private func updateTableViewHeight() {

    var height = goalFetchedResultsController.fetchedObjects!.count * 130

    height += 45

    if MyType.selected.hasMonthReport {
        height += 130
    }

    if MyType.selected.hasAnnualReport {
        height += 70
        height += 130
    }

    tableViewHeightConstraint.constant = CGFloat(height)
}

新单元格被添加到 NSFetchedResultsController 的委托(delegate)中。



Best Answer-推荐答案


我认为您需要更改 TableView 的 height 的计算方式,例如,

var height = tablView.contentSize.heigh // You can do + or - here 

// your other stuff of calculation.
tableViewHeightConstraint.constant = CGFloat(height)

self.view.layoutIfNeeded() // You need to call after change constraint.

关于ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47301428/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4