我在另一个集合视图单元格内使用一个集合视图。现在,我想滚动到内部集合View的indexPath。如果您知道的话,请帮助我。swift - 集合 View 单元格(嵌套的集合 View )内部的集合 View 的ScrollTo indexPath-LMLPHP

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

最佳答案

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

假设您将包含内部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 - 集合 View 单元格(嵌套的集合 View )内部的集合 View 的ScrollTo indexPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48106222/

10-14 21:03
查看更多