我尝试实施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!
答案 0 :(得分:0)
我设法用这个
获得正确的indexPathprotocol 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
}
}