在自定义UICollectionView中执行Segue嵌入在UITableView中

时间:2018-08-29 16:33:07

标签: ios swift xcode uitableview uicollectionview

我有一个具有自定义单元格的UITableView,其中包括一个水平滚动的UICollectionView,类似于Netflix App。每当用户单击UICollectionViewCell时,我都尝试执行segue。但是,performSegue(withIdentifier: )在“集合视图”单元格中不可用。

enter image description here

我尝试使用委托来处理segue:

在我的视图控制器类中:

func segueToNext(identifier: String) {
        performSegue(withIdentifier: identifier, sender: self)
    }

在UITableViewCell类中,我具有“集合视图”委托方法,并且具有以下代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    delegate?.segueToNext(identifier: "showDiningServices")
}
单击单元格时未调用

segueToNext。还有其他方法可以做到这一点吗?谢谢!

2 个答案:

答案 0 :(得分:2)

在VC内部

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = //////
   cell.delegate = self
} 

//

要使其正常工作,您必须设置

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    delegate?.segueToNext(identifier: "showDiningServices")
}

//

self.collectionView.delegate = self // self here is UITableViewCell

OR

您还可以在VC之内实现collectionView委托和dataSource,例如

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = //////
   cell.collectionView.delegate = self
   cell.collectionView.dataSource = self
} 

然后直接在VC中实现

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     performSegue(withIdentifier: "showDiningServices", sender: self)  
}

答案 1 :(得分:0)

您可以尝试使用导航控制器(使用pushViewController方法)推送视图控制器。当用户单击集合视图单元格时,将调用集合视图委托didSelectItemAtIndexPath,并且可以在该委托方法中执行视图控制器的推送。 / p>