我在 tableview 单元格中有一个 Collection View ,所以我想选择 Collection View 单元格并将它转到另一个 View Controller 。那我该怎么做呢?
我自己尝试过,它要么什么都不做,“应用程序试图以模态方式呈现事件 Controller ”,或者“尝试呈现 .. on .. who view is not in the window hierarchy”。
最佳答案
在单元类(在单元类之外)中添加一个委托(delegate)
,并在类中声明变量:
protocol CellSelectedDelegate { //Name them as you want
func cellSelected()
}
class TableCell: UITableViewCell {
var delegate: CellSelectedDelegate?
}
然后在单元格的 didSelectItem
中:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.cellSelected()
}
现在转到 controller 类,您有 tableView 数据源
和 delegate
方法(假设它们在 Controller 类中而不是在另一个 View 中)并将其添加到 cellForItem 方法中:
cell.delegate = self
最后一部分,在 Controller 类中实现自定义委托(delegate)
方法:
extension YourController: CellSelectedDelegate {
func cellSelected() {
//Present next controller here
}
}
关于ios - 从自定义 TableViewCell 中的 didSelectItemAt 自定义 CollectionViewCell 更改 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312638/