我在tableview单元格中有一个collection视图,所以我想选择collection视图单元格并将其转到另一个视图控制器。那我该怎么做呢?
我试过自己,它要么什么都不做,“应用程序试图以模式呈现一个活动控制器”,要么“试图呈现。。在。。其视图不在窗口层次结构中”。

最佳答案

在单元格类(单元格类外部)中添加delegate,并在类内部声明变量:

protocol CellSelectedDelegate { //Name them as you want
    func cellSelected()
}
class TableCell: UITableViewCell {
    var delegate: CellSelectedDelegate?
}

然后在细胞内:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
   delegate?.cellSelected()
}

现在转到controller类,其中有didSelectItemtableView datasource方法(假设它们在controller类中,而不是在另一个视图中),并将其添加到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/

10-12 00:15
查看更多