【问题标题】:Swift CollectionView Cell in a CollectionView Cell - Present New View ControllerCollectionView 单元中的 Swift CollectionView 单元 - 呈现新的视图控制器
【发布时间】:2020-07-28 19:37:55
【问题描述】:

大家好,

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

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

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

我已经尝试过这里的建议:Presenting a ViewController from within a CollectionViewCell that is nested in a TableViewCell,但“collectionview 单元格的嵌套”让我失望。

欣赏洞察力!

以下代码已更新

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

【问题讨论】:

  • 在 selectItme 函数中显示一些代码
  • 如果您在tableView 中使用collectionView,您应该使用委托,否则您将不得不存储ViewController 实例。请显示一些代码以阐明您到目前为止所做的尝试。
  • @Rob - 我已经更新了请求的代码。

标签: ios swift uicollectionview


【解决方案1】:

将答案更新为您的代码,您必须像这样向 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 的代码以及您使用什么标识符,所以我发布了一个简单的方法,使用 index path.row 作为 segue 标识符的简单字符串。如果玩具需要将数据传递给下一个 vc,也可以调整您的需求。考虑到。 PD。在您的 About 单元格中,您有一个委托 var,它是 Section 单元格类,您需要一种方法,即 Section 单元格类在从 about 单元格接收到操作后告诉 VC 它必须执行一个操作,并且您可以通过闭包来执行此操作,还有另一种方法可以在 viewController 中为 Section 单元格使用另一个委托,但我更喜欢闭包。

【讨论】:

  • 我已经更新了答案,包括一些代码和一些解释。
  • 处理我的 segues 的代码将是上面代码中 Section Cell 中的“switch 语句”。对于 AboutCell(item 1) = "goToNewVC1",对于 AboutCell(item 2) = "goToNewVC2",等等...
  • 我已将 HomeVC cellForItem 中的 performSegue 调用替换为以下内容:“self.aboutCategorySelected(val)”。这调用了我的基于 indexPath 的 segues。感谢您的帮助!
  • 很高兴它有帮助,哦,是的,这可能是但在你的询问中,aboutCategorySelected 函数在单元格中而不是在 ViewController 中,但很好的想法是在那里处理下一个 vc 的操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多