OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCell 里面的 UICollectionView 默认 contentOffset [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:18
标题: ios - UITableViewCell 里面的 UICollectionView 默认 contentOffset

在我的应用中,我有一个 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)
}



Best Answer-推荐答案


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

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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4