在自定义TableViewCell内从didSelectItemAt自定义CollectionViewCell更改视图控



我在tableview单元格中有一个集合视图,所以我想选择集合视图单元格,然后它转到另一个视图控制器。那么我该怎么做呢?

我自己也试过,它要么什么都不做,要么是"应用程序试图以模式方式呈现活动控制器",要么"试图在..上呈现..,其视图不在窗口层次结构中"。

单元格类(单元格类外部(中添加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()
}

现在转到控制器类,其中有tableView datasourcedelegate方法(假设它们在控制器类中,而不在另一个视图中(,并将其添加到cellForItem方法中:

cell.delegate = self

最后,在控制器类中实现custom delegate方法

extension YourController: CellSelectedDelegate {
func cellSelected() {
//Present next controller here
}
}

最新更新