如何从嵌套在TableViewCell中的CollectionViewCell和位于ViewController中的TableViewCell中呈现ViewController?
我试过presentViewController
和performSegue
但它们不能在细胞内被调用。
详细说明:
我有三份文件。
配置文件视图控制器
人员类别TableViewCell
人员类别集合视图单元格
当有人点击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")
}
最佳答案
像这样使用协议和委托
在tableViewCell
protocol CellDelegate {
func colCategorySelected(_ indexPath : IndexPath)
}
变量委托: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/