【发布时间】:2026-01-09 06:50:01
【问题描述】:
我尝试实现UITapGestureRecognizer 以从tableView 中获取collectionView 的indexPath.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 类,其中 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())
}
}
我从这篇文章中得到了tapSwitchTableCollection 的方法:
点击here!
【问题讨论】:
-
你有tableview里面的collection view吗?
-
我在 collectionView 中有一个 tableView,并想从其中的 tableView 中获取 indexPath.row。我无法使用 collectionView 中的 didSelectItemAt,因为我希望从 didSelectRowAt 中的 tableView 触发点击
-
我遇到了类似的问题,我在 tableView 中有一个完全一样的 collectionView。如果您熟悉 Objective C,我将向您展示一些代码,我是如何在不使用手势的情况下实现它的。如果您对我发布答案感兴趣,请告诉我。
-
感谢 Florentt,但我设法找到了解决方案
标签: ios uitableview swift3 uicollectionviewcell