【问题标题】:ScrollTo indexPath of a Collection View inside Collection view Cell (Nested CollectionView)ScrollTo indexPath 在 Collection View Cell 内的 Collection View (Nested CollectionView)
【发布时间】:2018-01-05 02:01:03
【问题描述】:

我在另一个集合视图单元格中使用集合视图。现在我想滚动到内部集合视图的 indexPath。如果你知道,请帮助我。

根据上图,我想将集合视图自动滚动到嵌套集合视图内的红色单元格。假设嵌套集合视图是 Main collectionView 中的一个部分。

【问题讨论】:

  • scrollToItem 可能是您想要的。 Scrolling to first cell 的可能重复项
  • scrollToItem 用于主集合视图。我想滚动到子集合视图索引路径 0,2 即上图中的红色单元格。
  • 是不是不能引用内部集合视图的情况?

标签: swift uicollectionview uicollectionviewcell collectionview


【解决方案1】:

尝试使用对此集合视图的引用。

假设你的 UICollectionView 包含内部 UICollectionView 被命名为 YourInnerCollectionViewCell,它看起来像:

let innerCollectionViewCellId = "innerCollectionViewCellId"

class MainCollectionView : UICollectionViewController {

    var innerCollectionViewCell: YourInnerCollectionViewCell?

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.delegate = self
        collectionView?.dataSource = self
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch indexPath.row {
        case 2:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: innerCollectionViewCellId, for: indexPath) as! YourInnerCollectionViewCell
            self.innerCollectionViewCell = cell
            return cell
        default:
            // return other cell
        }
    }

    ....

}

class YourInnerCollectionViewCell: UICollectionViewCell {

    override init(frame: CGRect){
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

现在您有了对该单元格的引用,您可以访问其中的 collectionView 并执行 scrollTo。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多