J. *_*Doe 3 core-data uitableview nsfetchedresultscontroller swift
我有一个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?
Run Code Online (Sandbox Code Playgroud)
您可以直接克隆示例项目: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)")
}
}
Run Code Online (Sandbox Code Playgroud)
如果我tableView.layoutIfNeeded在之后调用tableView.endUpdates(),该代表已被调用。调用委托方法会导致什么?有什么办法不滚动吗?
小智 6
我下载了您的代码,并进行了一些调整以解决故障问题。这是我所做的。
estimatedRowHeight表视图的属性设置为某个数字 init() {
super.init(frame: .zero, style: .plain)
estimatedRowHeight = 50.0
register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
}
Run Code Online (Sandbox Code Playgroud)
func reloadDataWithoutScroll() {
let lastScrollOffset = contentOffset
beginUpdates()
reloadData()
layoutIfNeeded()
endUpdates()
layer.removeAllAnimations()
setContentOffset(lastScrollOffset, animated: false)
}
Run Code Online (Sandbox Code Playgroud)
controllerDidChangeContent功能以使用reloadDataWithoutScroll功能 UIView.performWithoutAnimation {
tableView.performBatchUpdates({
tableView.insertRows(at: inserts, with: .automatic)
})
inserts.removeAll()
tableView.reloadDataWithoutScroll()
}
Run Code Online (Sandbox Code Playgroud)
当我执行这些更改时,添加新行时它不会滚动。但是,当当前contentOffset为0时,它会滚动以显示新添加的行。从逻辑上讲,我认为这不是问题。
| 归档时间: |
|
| 查看次数: |
145 次 |
| 最近记录: |