【问题标题】:Protocol doesn't work in collectionView Item协议在 collectionView 项目中不起作用
【发布时间】:2021-06-24 18:04:52
【问题描述】:

我有一个在 TableView Cell 中有几个项目的 collectionView,每个项目都应该推送到下一个 ViewController,问题是我不能使用 collectionView 和 tableView Cell 中的 push() 函数,所以我创建了一个当我用于单击 collectionView 项目时,该协议会从 Main ViewController 中触发推送功能。

这是协议:

protocol PushToAgents {
    
    func pushToAgents()
}

这是我从协议中实现功能的主要 Vc:

class MoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, PushToAgents, PushToBank, PushToCredit, PushToNewInserate {
    
    //MARK: - Protocol Functions
    
    func pushToAgents() {
        present(AgentsVC(), animated: true, completion: nil)
    }
}

这是 CollectionView:

class FeatureCollectionCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
    
    var agentsDelegate: PushToAgents?
    var bankDelegate: PushToBank?
    var creditDelegate: PushToCredit?
    var newInserateDelegate: PushToNewInserate?
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            agentsDelegate?.pushToAgents()
        }
        if indexPath.row == 1 {
            self.bankDelegate?.pushToBank()
        }
        if indexPath.row == 2 {
            self.creditDelegate?.pushToCredit()
        }
        if indexPath.row == 3 {
            self.newInserateDelegate?.pushToNewInserate()
        }
    }

问题是它什么也没做,谁能帮忙告诉我问题出在哪里?

【问题讨论】:

  • 您是否初始化了委托的属性?
  • FeatureCollectionCell 既是表格单元格又是集合视图委托和数据源?
  • @YuriiPetrov 你是什么意思?
  • @jnpdx 是的,它是一个带有 CollectionView 的 tableView 单元格!
  • 请添加表格视图的cellForRow方法

标签: swift uitableview uicollectionview


【解决方案1】:

视图层次应该是这样的

ViewController -> TableView -> TableViewCell -> CollectionView -> CollectionViewCell

所以数据传递应该是相反的。

ViewController <- TableView <- TableViewCell <- CollectionView <- CollectionViewCell

您需要将表格视图单元格设置为集合视图的委托。

【讨论】:

    【解决方案2】:
    protocol PushToAgents:class {
        func collectionView(collectioncell:CustomCollectionViewCell?, didTap TableCell:CustomTableViewCell)
    }
    

    //表格单元格配置

    weak var cellDelegate:PushToAgents? 
    
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath) as? CustomCollectionViewCell
            self.cellDelegate?.collectionView(collectioncell: cell, didTap: self)
        }
    

    // 在 cellForRow 中

    cell?.cellDelegate = self
    

    // 视图控制器配置

    extension ViewController:PushToAgents {
        func collectionView(collectioncell: CustomCollectionViewCell?, didTap TableCell: CustomTableViewCell) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      相关资源
      最近更新 更多