【发布时间】:2019-09-19 01:48:50
【问题描述】:
我有以下应用逻辑
TableView Controller => Table View Cell => CollectionView 嵌入在每个table view cell中
当我单击集合视图单元格时,我希望将表视图控制器与另一个控制器分隔开来。
有人知道怎么做吗?
这是我的代码:
在tableViewControler中
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)
}
【问题讨论】:
标签: ios swift uitableview tableview uicollectionviewcell