开发中遇到了view层级太多难以传值当前vc:
vc -> tableView -> cell -> collectionView -> cell
1、创建协议
protocol TestCellDelegate {
func didSelect(model:TestModel)
}
2、vc 遵守协议
class TestViewController: TestCellDelegate {
func didSelect(model:TestModel) {
print(model)
}
}
3、collectionViewDelegate里获取当前的vc,执行方法
extension TestTableViewCell : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let vc = self.getCurController() as? TestCellDelegate, let model = self.list[indexPath.row,true] {
vc.didSelect(model: model)
}
}
}
网友评论