ios - 单击嵌入在表格视图单元格中的集合视图单元格时继续
问题描述
我有以下应用程序逻辑
TableView Controller => Table View Cell => CollectionView 嵌入在每个表格视图单元格中
当我单击集合视图单元格时,我希望将表视图控制器与另一个控制器分隔开来。
有谁知道如何做到这一点?
这是我的代码:
在表视图控制器中
let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell
let books = (indexPath.row == 2) ? trendingbooks : newbooks
if (cell.collectionView === nil) {
cell.addCollectionView();
}
performSegue(withIdentifier: "bookDetail", sender: indexPath)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let dc = segue.destination as! DetailViewController
dc.data = self.tableRowData;
}
在 BookTableViewCell 中
func addCollectionView () {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
let cellWidth = UIScreen.main.bounds.width / 3
let cellHeight = UIScreen.main.bounds.height / 3
flowLayout.itemSize = CGSize(width: 100, height: self.frame.height)
//You can also provide estimated Height and Width
flowLayout.estimatedItemSize = CGSize(width: 100, height: self.frame.height)
//For Setting the Spacing between cells
flowLayout.minimumInteritemSpacing = 20
flowLayout.minimumLineSpacing = 20
let cellReuseIdentifier = "collectionCell"
self.collectionView = UICollectionView(frame: CGRect(x: 0,
y: 0,
width: self.frame.width,
height: self.frame.height),
collectionViewLayout: flowLayout)
self.collectionView.showsHorizontalScrollIndicator = false
self.collectionView.translatesAutoresizingMaskIntoConstraints = false
self.collectionView.backgroundColor = .clear
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(BookCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
self.addSubview(collectionView)
}
解决方案
我相信您正在寻找的功能是tableView(_:didSelectRowAt:)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegueWithIdentifier("bookDetail", sender: indexPath)
}
推荐阅读
- python - 与线程共享的内存
- php - pecl 无法创建锁定文件
- c++ - C ++函数中的char *参数是否会导致内存泄漏
- elasticsearch - 使用查询结果作为 Elasticsearch DSL 中另一个查询的参数
- django - 使用 Django Rest Framework TokenAuthentication 将令牌存储在 cookie 中
- javascript - Adding and removing checked of the second checkbox when the first is checked/unchecked
- deployment - 作为网络应用程序部署的绑定 Google Apps 脚本项目停止工作
- authentication - Safari 是否将 2 级域的子域视为第 3 方?
- reactjs - Mapbox 地图未在 ionic 生产版本中显示
- spring-boot - 如何处理春季启动中的每个异常(不是休息的东西)