如何访问嵌入在主UIViewController的父collectionViewCell中的collectionView? [英] How to access a collectionView that is embedded in a parent collectionViewCell in the main UIViewController?

查看:73
本文介绍了如何访问嵌入在主UIViewController的父collectionViewCell中的collectionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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


  1. 标题单元格

  2. 图像单元格

  3. 帖子单元格

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



我要访问 postCell,但我似乎无法通过委托获取此collectionView的引用。

  import UIKit 

协议HomeBannerPostsCellDelegate:类{
func homeBannerPostsCellDelegateMethod1(已启用:Bool)
}

类HomeBannerHeaderPostsCollectionCell:UICollectionViewCell {
@IBOutlet弱var bannerPostsCollectionView:UICollectionView!
var委托:HomeBannerPostsCellDelegate?
}

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

if(indexPath.item == 1 || indexPath.item == 3){
cell.backgroundColor = UIColor.blue
返回单元格
}
返回单元格
}

func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection部分: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
返回CGSize(width:screenWidth * 0.75,height:frameHeight)
}
}

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

  if(bannerCellReuseIdentifier == widgets [indexPath.item]){
let cell = collectionView.dequeueReusableCell( withReuseIdentifier:bannerCellReuseIdentifier,用于:indexPath)为! HomeBannerCollectionCell
cell.backgroundColor = UIColor.lightGray
cell.delegate = self
cell.bannerPostsDelegate = self
self.bannerPostsCollectionView = cell.bannerPostsCollectionView
返回单元格
}

在ViewDidLoad()方法中

 重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从笔尖进行其他任何设置。

//此视图控制器本身将为表视图提供委托方法和行数据。
collectionView.delegate =自我
collectionView.dataSource =自我

//为补充视图重用注册XIB
让XIB = UINib.init(nibName: SectionHeader ,包:Bundle.main)
collectionView.register(XIB,forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReuseIdentifier:HeaderIdentifier)

let layout = StickyHeadersCollectionViewFlowLayout()
collectionView.setCollectionViewLayout(layout,animationed :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(time间隔:3.0,目标:自我,选择器:#selector(autoChangeBanner),userInfo:无,重复: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?



预先感谢。

解决方案

在单元格上的主控制器确实选择了deligate方法,您将检查被窃听的单元格是否是带有横幅的单元格。

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

如果让bannerCell = cell as? HomeBannerHeaderPostsCollectionCell {
bannerCell.bannerPostsCollectionView ....
return
}

//这里您可以为其他单元格做任何事情
}

如果是这样,则将单元格强制转换为HomeBannerHeaderPostsCollectionCell,并从中访问其变量 cell .bannerPostsCollectionView ,然后您就可以做


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.

解决方案

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

这篇关于如何访问嵌入在主UIViewController的父collectionViewCell中的collectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆