【问题标题】:How to access a collectionView that is embedded in a parent collectionViewCell in the main UIViewController?如何访问嵌入在主 UIViewController 中的父 collectionViewCell 中的 collectionView?
【发布时间】:2018-03-02 07:35:14
【问题描述】:

如何访问嵌入在主 UIViewController 中的父 collectionViewCell 中的 collectionView?

我的主 ViewController 是一个 UICollectionView,其中包含一个名为 BannerCell 的 UICollectionViewCell。 BannerCell 有一个包含 3 个单元格的 collectionView:

  1. 标题单元格
  2. 图像单元格
  3. 帖子单元格

在 PostsCell 中,有一个包含帖子的单元格的 collectionView。

我想访问 postCell 中的集合视图,但我似乎无法通过委托获取此集合视图的引用。

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)
  }
}

我已经可以让 HomeBannerPostsCellDelegate 运行在主控制器中打印一行的方法。

   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
    }

在 ViewDidLoad() 方法中

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()

}

我正在尝试使用 setTimerMethod 让 PostsCollectionView 旋转

 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
    }
}

但bannerPostsCollectionView 为零。

有什么方法可以将bannerPostsCollectionView 的引用发送回MainController?

提前致谢。

【问题讨论】:

    标签: ios swift uicollectionview delegates uicollectionviewcell


    【解决方案1】:

    在单元格上的主控制器中选择了 deligate 方法,您将检查点击的单元格是否是带有横幅的单元格。

    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
        }
    

    如果是这样,您会将单元格转换为 HomeBannerHeaderPostsCollectionCell,然后您将访问它的变量 cell.bannerPostsCollectionView,然后您就可以做您想做的事了

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 2018-07-25
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多