UITableViewCell 里面的 UICollectionView 默认 contentOffset

Posted

技术标签:

【中文标题】UITableViewCell 里面的 UICollectionView 默认 contentOffset【英文标题】:UICollectionView inside UITableViewCell default contentOffset 【发布时间】:2017-11-15 16:51:40 【问题描述】:

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

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

但是这个 contentOffset 只适用于可见的表格单元格,当我滚动表格视图时,其他单元格没有这个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)

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

以上是关于UITableViewCell 里面的 UICollectionView 默认 contentOffset的主要内容,如果未能解决你的问题,请参考以下文章

从 UICollectionViewCell 中点击按钮时,如何引用 UITableViewCell?

UIView里面自定义UITableViewCell错误的宽度/高度

UITableViewCell 的动态高度和里面的内容

UITableViewCell里面的UIPageViewController

UITableViewCell 里面有一个 StackView,里面有动态添加的 WKWebViews 和 UIImageViews

UITollectionView在UITableViewCell里面