代码之家  ›  专栏  ›  技术社区  ›  Morteza Soleimani

UICollectionView内部UITableViewCell默认内容偏移量

  •  2
  • Morteza Soleimani  · 技术社区  · 7 年前

    表视图 包含多个单元格 UICollectionView ,UICollectionView的布局是自定义的,并在CellforRowatingIndexPath中设置。

    在里面 准备 集合视图布局 我将默认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)
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Morteza Soleimani    7 年前

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

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

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