【问题标题】:How to scroll to an IndexPath after apply the snapshot应用快照后如何滚动到 IndexPath
【发布时间】:2022-01-22 15:50:39
【问题描述】:
在应用集合视图快照后,我面临与滚动到 IndexPath 相关的问题。
为此,我编写了以下代码
dataSource.apply(snapshot, animatingDifferences: false, completion: {
self.scrollToIndex(self.visibleIndex)
})
不幸的是,它在 中不适合我
注意:适用于 iOS 15 及更高版本
【问题讨论】:
标签:
ios
swift
xcode
uicollectionview
uicollectionviewdiffabledatasource
【解决方案1】:
你可以用这个
self.collectionView.scrollToItem(at:IndexPath(item: index, section: 0), at: .right, animated: false)
【解决方案2】:
在短暂延迟后尝试执行滚动操作:
dataSource.apply(snapshot, animatingDifferences: false, completion: {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(10)) {[weak self] in
if let self = self, let indexToScrollTo = self.visibleIndex {
self.scrollToIndex(indexToScrollTo)
}
}
})