从 UITable 中获取 UICollectionView 的 indexPath

Get indexPath of UICollectionView from UITable within

我尝试实现 UITapGestureRecognizer 以从 tableView 中获取 collectionViewindexPath.row

我也试过委托,但好像不行。

protocol PassingCellDelegate {
    func passingIndexPath(passing: KitchenVC)
}

class ViewController: UIViewController {
    let tap =  UITapGestureRecognizer(target: self, action: #selector(tapSwitchTableViewCollection))

    var delegate: PassingCellDelegate?

    @IBAction func buttonCompletedTapped(sender: AnyObject) {
        delegate?.passingIndexPath(passing: self)
    }

    func tapSwitchTableCollection(sender: UITapGestureRecognizer) {

    if let cell = sender.view as? KitchenCollectionViewCell, let indexPath = collectionKitchen.indexPath(for: cell) {
        if self.selectedIndexPaths == indexPath {

            print("This in IF")
            print(indexPath)
            self.selectedIndexPaths = IndexPath()

        } else {

            print("This in ELSE")
            print(indexPath)
            self.selectedIndexPaths = indexPath

        }
    }
}

Cell class,其中 tableView 包含在 CollectionViewCell

class CollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource, PassingCellDelegate {

    func passingIndexPath(passing: KitchenVC) {

        passing.tapSwitchTableCollection(sender: passing.tap)

    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "OrdersCell")

        if tableView == ordersTableView {

            passingIndexPath(passing: KitchenVC())

        }

}

我从这个 post:

得到了 tapSwitchTableCollection 的方法

点击!

我设法用这个

获得了正确的 indexPath
protocol CustomCellDelegate { func passingIndexPath(passing: KitchenVC) }

    class ViewController: UIViewController {

        var delegate: PassingCellDelegate?

        @IBAction func buttonCompletedTapped(sender: AnyObject) {
            delegate?.passingIndexPath(passing: self)
        }

        func completedTapped(cell: KitchenCollectionViewCell) {

            var thisIndex = collectionKitchen.indexPath(for: cell)
            currentIndex = (thisIndex?[1])!

            // This returns indexPath.row from the collectionView
        }
    }