[英]change view controller from didSelectItemAt custom CollectionViewCell inside a custom TableViewCell
I have a collection view inside tableview cell, so I want to select the collection view cell and it goes to another view controller. 我在tableview单元格内有一个集合视图,因此我想选择集合视图单元格,然后转到另一个视图控制器。 So how do i do that?
那我该怎么做呢?
I have tried myself, it's either does nothing, "Application tried to present modally an active controller", or "Attempt to present .. on .. whose view is not in the window hierarchy". 我已经尝试过自己,它要么什么都不做,要么“应用程序试图以模态方式呈现一个活动的控制器”,要么是“试图以..形式呈现其视图不在窗口层次结构中的控件。”
Add a delegate
in cell class (Outside of cell class) and declare variable inside the class : 在单元格类 ( 单元格类外部)中添加一个
delegate
,并在类内声明变量:
protocol CellSelectedDelegate { //Name them as you want
func cellSelected()
}
class TableCell: UITableViewCell {
var delegate: CellSelectedDelegate?
}
Then in cell's didSelectItem
: 然后在单元格的
didSelectItem
:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.cellSelected()
}
Now go to controller class where you have tableView datasource
and delegate
methods (assuming they are in controller class and not in another view) and add this in cellForItem method : 现在去,你必须控制器类
tableView datasource
和delegate
方法(假设他们是在控制器类,而不是在另一个视图)和cellForItem方法补充一点:
cell.delegate = self
And last part, implement custom delegate
method in controller class : 最后一部分,在控制器类中实现
custom delegate
方法:
extension YourController: CellSelectedDelegate {
func cellSelected() {
//Present next controller here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.