单击嵌入在表格视图单元格中的集合视图单元格时继续

Posted

技术标签:

【中文标题】单击嵌入在表格视图单元格中的集合视图单元格时继续【英文标题】:segue when clicking on collection view cell embedded in table view cell 【发布时间】: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)
    

【问题讨论】:

【参考方案1】:

相信你要找的函数是tableView(_:didSelectRowAt:)

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

【讨论】:

当用户点击表格视图单元格中的集合视图单元格时,我如何让他们一直到表格视图控制器? 如果我正确理解您的问题,您需要确保目的地设置正确。正如有人在另一个答案中指出的那样,您应该确保正确地投射目标视图控制器,例如:let destination = segue.destination as? DetailViewController【参考方案2】:

我认为在你的情况下你可以像这样使用 segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == segueIdentifier,
       let destination = segue.destination as? DetailViewController,
       let selectedIndexPath = tableView.indexPathForSelectedRow,
       let selectedCell = tableView.cellForRow(at: indexPath) as? YourCell 
    
        let collectionView = selectedCell.collectionView
        // Then do your stuff here
    

【讨论】:

当我点击表格视图内的表格视图单元格中的集合视图单元格时,没有触发didSelectRowAt tableViewController函数,所以我无法调用performSegueIdentifier 您可能需要从您的集合视图单元格选择中进行回调,其中包含集合视图嵌入的表格视图单元格的信息。然后执行您的转场。【参考方案3】:

嵌入式集合视图的委托(BookTableViewCell)将首先听到选择。问题是 tableview 单元应该如何将选择传达给包含它的 VC。一个答案是在 BookTableViewCell 上定义一个委托,它将有关选择的信息从嵌入式集合视图传递到包含的 VC。

因此在配置BookTableViewCell 时,将其委托设置为包含它的VC,并让VC 实现此方法来执行segue。

protocol BookTableViewCell Delegate 
    func didSelectInEmbeddedCollection(cell: BookTableViewCell, at: IndexPath)

让单元格实现嵌入式集合视图的didSelectItem 方法,调用它didSelectInEmbeddedCollection

【讨论】:

我应该把 didSelectInEmbeddedCollection 放在哪里? 在自定义 BookTableViewCell 上定义协议和委托属性。配置单元时(出队后)将委托设置为 self(包含 vc)。然后,包含的 vc 必须实现 didSelectInEmbeddedCollection。它可以从那里触发segue 关键思想是集合视图比 VC 低两层,所以你需要两层委派。第一级由集合视图本身提供。第二个必须由自定义单元格提供。复杂,但它是您开始使用的复杂视图排列 当我尝试在 tableviewcell 上设置委托时出现错误,你知道为什么@danh吗? 我必须查看代码和错误。如果 VC 没有声明自己符合单元定义的协议,你会得到一个

以上是关于单击嵌入在表格视图单元格中的集合视图单元格时继续的主要内容,如果未能解决你的问题,请参考以下文章

使用单元格中的按钮转到详细视图控制器

如何通过单击更改集合视图单元格中的标签颜色

在集合视图单元格 Swift 中管理多个进度视图

单击表格单元格时将 UIViewController 滚动到顶部

为啥两个表格视图单元格中的两个集合视图在 Swift 4 中不起作用?

将数组中的图像嵌入到集合视图的单元格中