如何使用委托从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController?

Meh*_*l D 1 uitableview ios appdelegate uicollectionview swift

我正在尝试从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController。

我试图使用委托方法来实现,但它没有导航到预期的视图控制器。

这是我迄今为止开发的代码。我在这里使用 xib 设置。

//  ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,CustomTableViewCellDelegate {

    @IBOutlet weak var tableView: UITableView!
    var customTableViewCell = CustomTableViewCell()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        customTableViewCell.delegate = self
        
        tableView.delegate = self
        tableView.dataSource = self
        
        self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
    }

    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.barTintColor = UIColor.black
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
        self.navigationController?.navigationBar.tintColor = UIColor(red: 211/255, green: 86/255, blue: 50/255, alpha: 1.0)
    }
    
    //Delegate method
    func passTheCurrent(tableIndex: Int, collectionViewIndex: Int) {
        print("collectionViewIndex \(collectionViewIndex)")
        let selectpile = ObjectSceneViewCtrl()
        self.navigationController?.pushViewController(selectpile, animated: true)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        return customCell
    }

}
Run Code Online (Sandbox Code Playgroud)

这是 CustomTableViewCell,我在其中定义委托方法。我在 collectionview didSelectItemAt 方法中调用委托函数。但委托返回零。

import UIKit

protocol CustomTableViewCellDelegate {
    func passTheCurrent(tableIndex: Int, collectionViewIndex: Int)
}

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
   
    var delegate: CustomTableViewCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
}

extension CustomTableViewCell : UICollectionViewDelegate {}

extension CustomTableViewCell : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionViewCell
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
    }

}
Run Code Online (Sandbox Code Playgroud)

当我设置断点时,委托返回零。这个设置有什么问题。请帮帮我。

Muh*_*waz 5

执行以下简单步骤,这可能会对您有所帮助

1-在您的cellForRowAt方法中将委托分配给您的单元格

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
    customCell.delegate = self
    return customCell
    
}
Run Code Online (Sandbox Code Playgroud)

2-在您的CustomTableViewCell类中使用以下代码更新您的didSelectItemAt方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let del = self.delegate
    {
        del.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上我们现在正在做的事情是,首先我们将委托分配给每个索引上的单元格。在表格单元类中,我们首先检查委托是否已确认,然后将数据传递给父控制器类。