繁体   English   中英

Swift CollectionView 单元格中的 CollectionView 单元格 - 当前新视图 Controller

[英]Swift CollectionView Cell in a CollectionView Cell - Present New View Controller

在此处输入图像描述

大家好,

我在从嵌套的 CollectionView 单元格中呈现新视图的逻辑上遇到了问题。

在 HomeVC (cell1) 中,我有另一个集合视图嵌套了 4 个额外的单元格。 当用户单击相应的单元格时,我想将他们带到另一个视图 controller。图像中单元格 1 下的绿色箭头表示工作流。

实现这一目标的最佳方法是什么?

我已经尝试过这里的建议: 从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现一个 ViewController但是“集合视图单元格的嵌套”让我失望。

感谢洞察力!

使用以下代码更新

//HomeVC.swift -  
class HomeVC: UIViewController {

extension HomeVC: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if indexPath.item == 0 {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SectionCell
        

        return cell
    }

//SectionCell.swift - Menu item 1
class SectionCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, CellDelegate {

func aboutCategorySelected(_ indexPath: IndexPath) {
    
    switch indexPath.item {
      case 0: //go to new VC1
      case 1: //go to new VC2
      case 2: //go to new VC3
      case 3: //go to new VC4
      default: break

    }
    

}

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.backgroundColor = .init(white: 0.9, alpha: 1)
    cv.delegate = self
    cv.dataSource = self
    
    return cv
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    //register nested cv cell
    collectionView.register(AboutCell.self, forCellWithReuseIdentifier: "aboutCell")
    
}

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

 if indexPath.item == 2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "aboutCell", for: indexPath) as! AboutCell
        
        cell.delegate = self
        
        return cell
    }
 }

 //AboutCell.swift - this is the nested cell
 protocol CellDelegate {

func aboutCategorySelected(_ indexPath : IndexPath)
}

 class AboutCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var delegate: CellDelegate?


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    delegate?.aboutCategorySelected(indexPath)
}

将答案更新为您的代码,您必须像这样向 sectionCell 添加一个闭包,并更新项目的单元格以响应闭包,并在这种情况下执行 segue。

    extension HomeVC: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if indexPath.item == 0 {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SectionCell
        cell.completion = {[weak self] val in
            guard let self = self else {return}
            self.performSegue(withIdentifier: val.row.description, sender: nil)
        }
        return cell
    }
}
//SectionCell.swift - Menu item 1
class SectionCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, CellDelegate {
    var completion: ((IndexPath)->Void)?

func aboutCategorySelected(_ indexPath: IndexPath) {
    completion?(indexPath)
}

因为你没有提供代码来处理 segue 以及你使用什么标识符我发布了一个简单的方法使用索引 path.row 作为 segue 标识符的简单字符串。 如果玩具需要将数据传递给下一个 vc,也可以根据您的需要进行调整。 考虑到。 PD。 在您的 About 单元格中,您有一个代表 var,它是 Section 单元格 class,您需要的是 class Section 单元格在收到来自 about 单元格的操作后告诉 VC 它必须执行一个操作并且您可以关闭它,还有另一种方法,在 viewController 中为 Section 单元格使用另一个委托,但闭包是我的首选方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM