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