2017-06-09 14 views
0

私はViewController内に2つのコレクションビューを持っています。 1つのコレクションビューは、上部のタブバーを表し、もう1つは、画面の下半分のページを表します。私はカスタムUIView(別のクラスからロード)内の2番目のUICollectionView(下半分)を持っています。私の目的は、タブバーをクリックすると、scrollToIndexに関して、それに応じて下部のcollectionViewを移動することです。私の問題は、上のタブバーをクリックして、私のカスタムUIViewにある関数を呼び出すと何も起こらないということです。デリゲート/プロトコルを通じて情報を共有する必要がありますか?私は間違って何をしていますか?私MainVCでUICollectionViewのScrollToItemで何も起こりません

私が持っている:そして、MainVCためMainVCためcollectionView(バーボタン)すると

func setUpHeaderBarButtons() { 
     let flowLayout = UICollectionViewFlowLayout() 
     let collectionView = UICollectionView(frame: CGRect(x: topBar.frame.width * 0, y:topBar.frame.height/1.75, width: topBar.frame.width, height: topBar.frame.height/3.6), collectionViewLayout: flowLayout) 
     collectionView.register(SelectionCVC.self, forCellWithReuseIdentifier: cellId) 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.backgroundColor = UIColor.clear 
     topBar.addSubview(collectionView) 

    } 

& & didSelect(バーボタンを設定

/** 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 

    } 

& )

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("selecting cell") 
     mainView.moveToPage() 
    } 

& &それから私は私が間違っているつもりです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) 

    } 
    } 

をトリガしたい下のコレクションビューで自分のカスタムUIViewの?代わりに、あなたはあなたがcollectionView2での参照を持っていない全く新しいcollectionViewを初期化しているcollectionView2インスタンスのプロパティを初期化するMainViewsHomeinit

答えて

1

。それは

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) 
でなければなりません

関連する問題