Swift3+Xcode8.3.3:UITableViewCell嵌套UICollectionView跳转问题
嘿,我来帮你搞定这个嵌套UICollectionView的跳转问题!核心思路是把CollectionViewCell的点击事件传递给外层的UITableViewController,让它来处理导航跳转(因为只有导航控制器的子VC才能直接调用push方法)。下面是具体的实现步骤,结合你的代码片段来补充:
第一步:给PastCell添加代理协议(或闭包)
先在PastCell里定义一个代理协议,用来传递点击事件:
import UIKit // 定义代理协议,用于传递CollectionViewCell的点击事件 protocol PastCellDelegate: AnyObject { func pastCell(_ cell: PastCell, didTapCollectionItemAt indexPath: IndexPath) } class PastCell: UITableViewCell { @IBOutlet weak var ptripDtls: UICollectionView! var logcount: Int = 0 // 代理对象,用weak避免循环引用 weak var delegate: PastCellDelegate? override func awakeFromNib() { super.awakeFromNib() ptripDtls.delegate = self ptripDtls.dataSource = self // 如果你的CollectionViewCell是纯代码创建的,记得注册cell // ptripDtls.register(YourCollectionCell.self, forCellWithReuseIdentifier: "YourCollectionCellID") } } // 实现UICollectionViewDataSource extension PastCell: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return logcount // 这里替换成你的实际数据源数量 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCollectionCellID", for: indexPath) as! YourCollectionCell // 这里配置你的CollectionViewCell数据,比如设置图片、文字等 return cell } } // 实现UICollectionViewDelegate,处理点击事件 extension PastCell: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { // 点击时通知代理 delegate?.pastCell(self, didTapCollectionItemAt: indexPath) } }
第二步:在主UITableViewController里实现代理
让你的主TableViewController遵循PastCellDelegate协议,然后处理跳转逻辑:
class MainTableViewController: UITableViewController, PastCellDelegate { // 假设你有数据源数组,用来给每个PastCell提供数据 var yourDataSource: [YourDataModel] = [] override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return yourDataSource.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PastCell", for: indexPath) as! PastCell // 设置代理 cell.delegate = self // 配置cell的logcount和其他数据 cell.logcount = yourDataSource[indexPath.row].logCount return cell } // 实现代理方法,处理跳转 func pastCell(_ cell: PastCell, didTapCollectionItemAt indexPath: IndexPath) { // 1. 先找到当前点击的PastCell在TableView里的位置 guard let tableViewIndexPath = tableView.indexPath(for: cell) else { return } // 2. 获取对应的数据源(根据你的业务逻辑调整) let currentData = yourDataSource[tableViewIndexPath.row] let collectionItemData = currentData.collectionItems[indexPath.item] // 3. 实例化目标ViewController guard let destinationVC = storyboard?.instantiateViewController(withIdentifier: "YourDestinationVC") as? YourDestinationViewController else { print("无法找到目标ViewController,请检查storyboard里的ID") return } // 4. 传递数据给目标VC(可选) destinationVC.itemData = collectionItemData // 5. 导航跳转 navigationController?.pushViewController(destinationVC, animated: true) } }
额外注意事项
- 确保导航控制器存在:你的主
MainTableViewController必须被嵌入在UINavigationController中,否则navigationController会是nil,无法跳转。 - Storyboard配置:记得给目标ViewController设置正确的Storyboard ID(比如上面的
YourDestinationVC),并且CollectionViewCell也要设置对应的Reuse Identifier。 - 闭包替代方案:如果你觉得代理太繁琐,可以用闭包来传递事件,把PastCell里的代理换成闭包属性:
// 在PastCell里替换代理为闭包 var didTapCollectionItem: ((IndexPath) -> Void)? // 然后在didSelectItemAt里调用 didTapCollectionItem?(indexPath) // 在cellForRowAt里设置闭包 cell.didTapCollectionItem = { [weak self] indexPath in guard let self = self else { return } // 同样的跳转逻辑... }
这样设置后,点击CollectionViewCell就会跳转到目标VC,用户点击返回按钮就能回到主TableViewController啦!
内容的提问来源于stack exchange,提问作者Rakesh Kumar
相关产品推荐
相关产品推荐

