在我的应用程序中,我有一个 tableView ,其中包含几个带 UICollectionView 的单元格,UICollectionView的布局是自定义的,并在cellForRowAtIndexPath中设置。
在 collectionViewLayout 的准备方法中,我将默认contentOffset设置为collectionView。
但是这个contentOffset仅适用于可见表格单元格,当我滚动tableview时,其他单元格没有default content offset
。
如何解决此问题?
override func prepare() {
guard cache.isEmpty, let collectionView = collectionView else {
return
}
// ...
// Prepare cell attributes and add to cache array
// ...
collectionView.contentOffset = CGPoint(x: 100, y:0)
}
答案 0 :(得分:0)
我使用以下解决方案来解决此问题,在几毫秒延迟后设置内容偏移修复了此问题。
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(10), execute: {
collectionView.contentOffset = self.initialContentOffset!
})
但我认为这不是最佳解决方案,但我找不到解决此问题的其他解决方案