How to access a collectionView that is embedded in

2019-08-27 01:03发布

问题:

How to access a collectionView that is embedded in a parent collectionViewCell in the main UIViewController?

My main ViewController is a UICollectionView which houses a UICollectionViewCell called BannerCell. BannerCell has a collectionView which contains 3 cells:

  1. Headlines cell
  2. Image cell
  3. Posts Cell

Within PostsCell, there is a collectionView of cells which contains posts.

I want to access the collection view within the postCell, but I can't seem to get the reference of this collectionView through a delegate.

import UIKit

protocol HomeBannerPostsCellDelegate: class {
  func homeBannerPostsCellDelegateMethod1(enabled: Bool)
}

class HomeBannerHeaderPostsCollectionCell: UICollectionViewCell {
   @IBOutlet weak var bannerPostsCollectionView: UICollectionView!
   var delegate: HomeBannerPostsCellDelegate?
}

extension HomeBannerHeaderPostsCollectionCell : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeBannerHeaderPostCollectionCell", for: indexPath) as! HomeBannerHeaderPostCollectionCell
      cell.backgroundColor = UIColor.red

      if(indexPath.item==1 || indexPath.item==3 ){
          cell.backgroundColor = UIColor.blue
          return cell
      }
      return cell
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenWidth = UIScreen.main.bounds.width
    let frameHeight = screenWidth*0.10
    return CGSize(width: screenWidth*0.75, height: frameHeight)
  }
}

I can already get the HomeBannerPostsCellDelegate to run the method that prints out a line in the main controller.

   if(bannerCellReuseIdentifier == widgets[indexPath.item]){
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bannerCellReuseIdentifier, for: indexPath) as! HomeBannerCollectionCell
        cell.backgroundColor = UIColor.lightGray
        cell.delegate=self
        cell.bannerPostsDelegate=self
        self.bannerPostsCollectionView=cell.bannerPostsCollectionView
        return cell
    }

In ViewDidLoad() method

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

        // This view controller itself will provide the delegate methods and row data for the table view.
    collectionView.delegate = self
    collectionView.dataSource = self

    // Register XIB for Supplementary View Reuse
    let XIB = UINib.init(nibName: "SectionHeader", bundle: Bundle.main)
    collectionView.register(XIB, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HeaderIdentifier)

    let layout = StickyHeadersCollectionViewFlowLayout()
    collectionView.setCollectionViewLayout(layout, animated: true)
    collectionView.backgroundColor = UIColor.white

    collectionView.contentInset = UIEdgeInsets(top: -collectionView.bounds.width * 0.20 * 2, left: 0, bottom: 0, right: 0)

    self.homeBannerPostsCellDelegateMethod1(enabled: true)

    setTimer()

}

I'm trying to get the PostsCollectionView to rotate using the setTimerMethod

 func setTimer() {
    let _ = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(autoChangeBanner), userInfo: nil, repeats: true)
}

var bannerPostIndex = 0
@objc func autoChangeBanner() {
    NSLog("autoChangeBanner")
    let indexPath = IndexPath(item: bannerPostIndex, section: 0)
    self.bannerPostsCollectionView?.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
    bannerPostIndex=bannerPostIndex+1
    if(bannerPostIndex>3){
        bannerPostIndex=0
    }
}

But bannerPostsCollectionView is nil.

Is there any way I can send the reference of bannerPostsCollectionView back to the MainController?

Thanks in advance.

回答1:

In the main Controller on cell did select deligate method you will check if the tapped cell is the cell with the banner.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

        if let bannerCell = cell as? HomeBannerHeaderPostsCollectionCell {
              bannerCell.bannerPostsCollectionView ....
              return
          }

        //And here you do whatever you want for the other cells
    }

If so you will cast the cell as a HomeBannerHeaderPostsCollectionCell and from that you will access it's variable cell.bannerPostsCollectionView and you can do what you want