集合视图内表视图委托

时间:2019-06-06 06:58:19

标签: swift

我有一个表格视图,表格行内有集合视图。 结构是下一个:

MainViewController.swift:

class MainViewController: UIViewController {
     @IBOutlet weak var customTable: UITableView!
     func callSegue() {
         performSegue(withIdentifier: "customSegue", sender: self)
     }
     override func viewDidLoad() {
          customTable(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "TipsTableCell")
     }
}
extension MainViewController: UITableViewDataSource {
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return 1
     }
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
          //Fill cell with my data
          return cell
     }
}

CustomTableCell.swift

class CustomTableCell.swift: UITableViewCell {
     @IBOutlet var collectionView: UICollectionView!
     override func awakeFromNib() {
         super.awakeFromNib()
         self.collectionView.dataSource = self
         self.collectionView.delegate = self
         self.collectionView.register(UINib.init(nibName: "CustomTableCell", bundle: nil), forCellWithReuseIdentifier: "CustomTableCell")
     }
}
extension CustomTableCell: UICollectionViewDataSource, UICollectionViewDelegate {
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
 cell.label1.text = dataArray[indexPath.item]
return cell

和我的CustomCollectionvCell.swift

class CustomCollectionvCell: UICollectionViewCell {
     @IBOutlet weak var label1: UILabel!
     override func awakeFromNib() {
         super.awakeFromNib()
     }

我需要这样的东西: 当我在label1.text ==“ Something”的单元中点击时,我需要在MainViewController中调用“ callSegue”函数。

2 个答案:

答案 0 :(得分:0)

在您的情况下,您必须从CustomTableCell.swift实现委托并在MainViewController.swift中使用

// MainViewController.swift:

class MainViewController: UIViewController {
    @IBOutlet weak var customTable: UITableView!
    func callSegue() {
        performSegue(withIdentifier: "customSegue", sender: self)
    }
    override func viewDidLoad() {
        customTable(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "TipsTableCell")
    }
}


extension MainViewController: UITableViewDataSource, collectionViewCellTapped {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
        //Fill cell with my data
        return cell
    }

    func cellTapped(_ text: String) {
        if let text = dataArray[indexPath.item], text == "Something" {
            callSegue()
        }
    }

}

// CustomTableCell.swift
protocol collectionViewCellTapped {
    func cellTapped(_ text: String)
}

class CustomTableCell : UITableViewCell {
    @IBOutlet var collectionView: UICollectionView!
    var delegate: collectionViewCellTapped!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.register(UINib.init(nibName: "CustomTableCell", bundle: nil), forCellWithReuseIdentifier: "CustomTableCell")
    }
}

extension CustomTableCell: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
        cell.label1.text = dataArray[indexPath.item]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let delegate = self.delegate {
            delegate.cellTapped(text)
        }
    }
}

答案 1 :(得分:0)

使用 closures 解决该问题。

closure中添加一个CustomTableCell,并在collectionViewCell方法中点击collectionView(_:didSelectItemAt:)时调用它,即

class CustomTableCell: UITableViewCell {
    var handler: (()->())?
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.handler?()
    }
}

MainViewController中,设置closure的同时以CustomTableCell方法(即

)将tableView(_:cellForRowAt:)出队。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
    cell.handler = {[weak self] in
        self.callSegue() //here.....
    }
    return cell
}

还要交叉检查segue中是否有一个标识符为customSegue的{​​{1}}。