如何从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController,而 TableViewCell 位于 ViewController 中?
我已经尝试过 presentViewController
和 performSegue
但它们无法从单元格中调用。
详细解释:
我有三个文件。
- 配置文件 View Controller
- PeopleCategoryTableViewCell
- PeopleCategoryCollectionViewCell
当有人点击 CollectionViewCell 中的故事时,我希望他们重定向到另一个 ViewController,即 SinglePostVC()
我尝试过的:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("inside this")
self.window?.rootViewController?.present(SinglePostVC(), animated: true, completion: nil)
print("outside this")
}
最佳答案
像这样使用协议(protocol)和委托(delegate)
在tableViewCell
中
protocol CellDelegate {
func colCategorySelected(_ indexPath : IndexPath)
}
var delegate : CellDelegate?
在didSelect中
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.colCategorySelected(indexPath)
}
在你的 ProfileViewController 中
class HomeVC: UIViewController , UITableViewDelegate, UITableViewDataSource, CellDelegate{
:
:
func colCategorySelected(_ indexPath : IndexPath){
// Push here
}
:
:
}
别忘了
cellForRow
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! tableCell
Cell.delegate = self // dont forget this line.
return Cell
关于ios - 从嵌套在 TableViewCell 中的 CollectionViewCell 中呈现 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404897/