用奇怪的contentOffsetY调用的scrollViewDidScroll

时间:2019-03-23 15:22:39

标签: swift uitableview core-data nsfetchedresultscontroller

我有一个UIViewController,其中有一个NSFetchedResultsController。在将行插入到顶部之后,我想让行在插入之前的位置保持可见。这意味着我需要进行一些计算以在更新后立即将contentOffSetY保持不变。计算是正确的,但是我注意到scrollViewDidScroll在滚动到我指定的contentOffsetY之后被调用,这导致状态损坏。这是日志记录:

Will apply an corrected Y value of: 207.27359771728516
Scrolled to: 207.5
Corrected to: 207.27359771728516
Scrolled to: 79.5 <-- Why is this logline here?

您可以直接克隆示例项目:https://github.com/Jasperav/FetchResultControllerGlitch(提交https://github.com/Jasperav/FetchResultControllerGlitch/commit/d46054040139afeeb648e1e0b5b113bd98685b4a该项目的最新版本仅会出现故障,对scrollViewDidScroll方法的怪异调用现在消失了。如果修复我授予赏金的小故障。只需克隆最新版本,运行它并滚动一点。您会看到奇怪的内容偏移量(故障))。运行项目,您将看到奇怪的输出。这是controllerDidChangeContent方法:

public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    let currentSize = tableView.contentSize.height

    UIView.performWithoutAnimation {
        tableView.endUpdates()

        let newSize = tableView.contentSize.height
        let correctedY = tableView.contentOffset.y + newSize - currentSize

        print("Will apply an corrected Y value of: \(correctedY)")
        tableView.setContentOffset(CGPoint(x: 0,
                                           y: correctedY),
                                   animated: false)
        print("Corrected to: \(correctedY)")
    }
}

如果我在tableView.layoutIfNeeded之后立即致电tableView.endUpdates(),则已经调用了该委托。调用委托方法会导致什么?它有什么不滚动的方式吗?

1 个答案:

答案 0 :(得分:0)

我下载了您的代码,并进行了一些调整以解决故障问题。这是我所做的。

  1. 将表视图的estimatedRowHeight属性设置为某个数字
    init() {
        super.init(frame: .zero, style: .plain)
        estimatedRowHeight = 50.0
        register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
    }
  1. 创建了一个无需修改当前contentOffset即可处理UITableView重新加载操作的函数
    func reloadDataWithoutScroll() {
        let lastScrollOffset = contentOffset
        beginUpdates()
        reloadData()
        layoutIfNeeded()
        endUpdates()
        layer.removeAllAnimations()
        setContentOffset(lastScrollOffset, animated: false)
    }
  1. 更新了controllerDidChangeContent函数以使用reloadDataWithoutScroll函数
    UIView.performWithoutAnimation {
        tableView.performBatchUpdates({
            tableView.insertRows(at: inserts, with: .automatic)
        })   
        inserts.removeAll()
        tableView.reloadDataWithoutScroll()
    }

当我执行这些更改时,添加新行时它不会滚动。但是,当当前contentOffset为0时,它会滚动以显示新添加的行。从逻辑上讲,我认为这不是问题。

相关问题