Swift UICollectionView中的ScrollToItem未发生任何事件

Swift UICollectionView中的ScrollToItem未发生任何事件,swift,uiview,swift3,uicollectionview,protocols,Swift,Uiview,Swift3,Uicollectionview,Protocols,我在ViewController中有两个集合视图。一个集合视图表示顶部的选项卡栏,另一个表示屏幕下半部分的页面。我在自定义UIView(从另一个类加载)中有第二个UICollectionView(在下半部分)。我的目标是在单击选项卡栏时,根据scrollToIndex相应地移动底部collectionView。我的问题是,当我单击顶部的选项卡栏并调用自定义UIView中的函数时,什么都没有发生。我是否需要通过代表/协议共享信息?我做错了什么 在我的主要VC中,我有: /** Setting up

我在ViewController中有两个集合视图。一个集合视图表示顶部的选项卡栏,另一个表示屏幕下半部分的页面。我在自定义UIView(从另一个类加载)中有第二个UICollectionView(在下半部分)。我的目标是在单击选项卡栏时,根据scrollToIndex相应地移动底部collectionView。我的问题是,当我单击顶部的选项卡栏并调用自定义UIView中的函数时,什么都没有发生。我是否需要通过代表/协议共享信息?我做错了什么

在我的主要VC中,我有:

/** Setting up the top header **/


 let topBar = UIView()
    /** Setting up the connection to the Bottom Collection View **/
    let mainView = MainViewsHome()

    override func viewWillAppear(_ animated: Bool) {
           self.view.layoutIfNeeded()
        /**Setting up the header that the buttons lay on **/
        topBar.frame = CGRect(x:self.view.frame.width * 0, y:self.view.frame.height * 0, width:self.view.frame.width,height:self.view.frame.height / 6.2)
        topBar.backgroundColor = UIColor.red
        self.view.addSubview(topBar)

        /** Setting up the buttons in the header**/
        setUpHeaderBarButtons()

        /** Setting up the bottom half **/
        mainView.frame = CGRect(x:self.view.frame.width * 0, y:self.view.frame.height / 6.2, width:self.view.frame.width,height:self.view.frame.height / 1.1925)
        mainView.backgroundColor = UIColor.clear
        self.view.addSubview(mainView)
        mainView.delegate = self

    }
&然后设置MainVC的collectionView(条形按钮)

&&然后为MainVC选择DID(条形按钮)

&&然后是我的自定义UIVIew和我希望触发scrollToIndex的底部集合视图

class MainViewsHome: UIView, UIGestureRecognizerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    var cellId = "Cell"

    var collectionView2 : UICollectionView!

     override init(frame: CGRect) {

        super.init(frame: CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 1.27))

        /**Creation of the View **/
        let flowLayout2 : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        flowLayout2.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        flowLayout2.scrollDirection = .horizontal

        let collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)
        collectionView2.register(SelectionCVC.self, forCellWithReuseIdentifier: cellId)
        collectionView2.isPagingEnabled = true
        collectionView2.delegate = self
        collectionView2.dataSource = self
        collectionView2.backgroundColor = UIColor.purple

        self.addSubview(collectionView2)

    }


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

    func moveToPage() {
     //   print("we are moving to page")

        let indexPath = IndexPath(item: 2, section: 0) //Change section from 0 to other if you are having multiple sections
        self.collectionView2?.scrollToItem(at: indexPath, at: [], animated: true)

    }
    }

我哪里出错了?

MainViewsHome
init
中,不是初始化实例属性
collectionView2
而是初始化全新的
collectionView
,因此在
collectionView2
中没有引用

应该是

self.collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)
而不是

let collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)
self.collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)
let collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)