gpt4 book ai didi

swift - Collection View 单元格内 Collection View 的 ScrollTo indexPath(嵌套 CollectionView)

转载 作者:行者123 更新时间:2023-11-30 11:53:24 24 4
gpt4 key购买 nike

我正在另一个 Collection View 单元格内使用 Collection View 。现在我想滚动到内部 Collection View 的索引路径。如果你知道这一点,请帮助我。 Nested CollectionView - Need scroll to red cell

根据上图,我想将 Collection View 自动滚动到嵌套 Collection View 内的红色单元格。假设嵌套 Collection View 是主 Collection View 中的一个部分。

最佳答案

尝试使用对此 Collection View 的引用。

假设包含内部 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。

希望有帮助。

关于swift - Collection View 单元格内 Collection View 的 ScrollTo indexPath(嵌套 CollectionView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48106222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com