菜鸟教程小白 发表于 2022-12-11 19:18:16

ios - UITableViewCell 里面的 UICollectionView 默认 contentOffset


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

<p>在 <strong>collectionViewLayout</strong> 的 <strong>prepare</strong> 方法中,我将默认 contentOffset 设置为 collectionView。</p>

<p>但是这个 contentOffset 只适用于可见的表格单元格,当我滚动表格 View 时,其他单元格没有这个 <code>默认内容偏移量</code>。</p>

<p>如何解决这个问题?</p>

<pre><code>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)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我使用以下解决方案解决了这个问题,在几毫秒延迟后设置内容偏移解决了这个问题。</p>

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

<p>但我认为这不是最好的解决方案,但我找不到解决此问题的其他解决方案</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCell 里面的 UICollectionView 默认 contentOffset,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47313048/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47313048/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCell 里面的 UICollectionView 默认 contentOffset