首页 > 解决方案 > 单击嵌入在表格视图单元格中的集合视图单元格时继续

问题描述

我有以下应用程序逻辑

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)
    }

标签: iosswiftuitableviewtableviewuicollectionviewcell

解决方案


我相信您正在寻找的功能是tableView(_:didSelectRowAt:)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegueWithIdentifier("bookDetail", sender: indexPath)
}

推荐阅读