从UITable中获取UICollectionView的indexPath

时间:2017-01-18 20:18:42

标签: ios uitableview swift3 uicollectionviewcell

我尝试实施UITapGestureRecognizer以从indexPath.row获取collectionView的{​​{1}}。

我也试过了代表团,但似乎没有用。

tableView

Cell类,其中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 } } } 包含在CollectionViewCell

tableView

我从这篇文章中得到了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()) } } 的方法:

点击here

1 个答案:

答案 0 :(得分:0)

我设法用这个

获得正确的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
        }
    }