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

标签 ios swift xcode uitableview uicollectionview

我有一个 UITableView,它有自定义单元格,其中包括一个水平滚动的 UICollectionView,类似于 Netflix 应用程序。每当用户单击 UICollectionViewCell 时,我都会尝试执行 segue。但是,performSegue(withIdentifier:) 在 Collection View Cells 中不可用。

enter image description here

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

在我的 View Controller 类中:

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

在 UITableViewCell 类中,我有 Collection View 委托(delegate)方法,我有以下代码:

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

segueToNext 在我点击单元格时没有被调用。还有其他方法可以做到这一点吗?谢谢!

最佳答案

风投内部

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

你也可以在 VC 中实现 collectionView delegate 和 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)  
}

关于ios - 在 UITableView 中嵌入的自定义 UICollectionView 中执行 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52082237/

相关文章:

ios - 是否可以通过单击 Xcode 5 中的构建和运行按钮在两个设备上运行应用程序?

ios - 何时使用 sceneDidLoad v didMove(查看 :)

ios - 如何在滚动 iOS 上显示导航栏

swift - 谁能告诉我为什么我的过滤数组是空的?

iOS 应用程序和 Facebook "user_photos"权限

ios - 更改照片库按钮的颜色

ios - 删除 AVAudioSessionPortBuiltInMic 作为选项

ios - 在 Swift 中使用协议(protocol)和委托(delegate)将数据从 UITableViewDataSource 传递到 UIViewController

ios - WKWebView 不与状态栏齐平

ios - 如何在向后导航中获取 View Controller Storyboard ID 或 restortionIdentifier?