草庐IT

ios - UICollectionView inside UITableViewCell default contentOffset

coder 2024-01-28 原文

在我的应用程序中,我有一个 tableView,其中包含多个带有 UICollectionView 的单元格,UICollectionView 的布局是自定义的,并在 cellForRowAtIndexPath 中设置。

collectionViewLayoutprepare 方法中,我将默认的 contentOffset 设置为 collectionView。

但此 contentOffset 仅适用于可见的表格单元格,当我滚动表格 View 时,其他单元格没有此默认内容偏移量

如何解决这个问题?

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)
}

最佳答案

我使用以下解决方案解决了这个问题,延迟几毫秒后设置内容偏移量解决了这个问题。

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(10), execute: {
    collectionView.contentOffset = self.initialContentOffset!
})

但我认为这不是最好的解决方案,但我找不到解决此问题的另一种解决方案

关于ios - UICollectionView inside UITableViewCell default contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47313048/

有关ios - UICollectionView inside UITableViewCell default contentOffset的更多相关文章

随机推荐