[英]How to show ViewController under the didSelectItemAt indexPath function ? (without storyboard)
Have a nice weekend. 周末愉快。 I have problem about that under the didSelectItemAt indexPath function I want to go new view controller which is show details and each cell must have different view controller (it will change according to data which are came from protocol).
我在didSelectItemAt indexPath函数下遇到问题,我想转到显示详细信息的新视图控制器,并且每个单元格必须具有不同的视图控制器(它将根据来自协议的数据进行更改)。 I tried lots of way to do that but I can't figured out.
我尝试了很多方法来做到这一点,但我不知道。 I call present and push view controller codes but its also not working because navigationController code can't work under didSelectItemAt function.
我调用了present和push视图控制器代码,但它也无法正常工作,因为navigationController代码在didSelectItemAt函数下无法工作。 Lastly I tried that:
最后,我尝试了:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let homecon = HomeController()
homecon.showDetailPage() }
func showDetailPage() {
let newView = pageDetail()
self.present(newView, animated: true, completion: nil)
}
(showDetailPage function from HomeController). (来自HomeController的showDetailPage函数)。 But that code also gave error which is
但是该代码也给出了错误
'UICollectionView must be initialized with a non-nil layout parameter'.
“ UICollectionView必须使用非nil布局参数初始化”。
So I can't solve it. 所以我解决不了。 How can I solve it please help me.
我该如何解决,请帮助我。 Have a nice work!
辛苦了!
This is because HomeController
is a subclass of UICollectionViewController
and a collectionViewController can not be initialized without it's layout! 这是因为
HomeController
是UICollectionViewController
的子类,并且没有其布局就无法初始化collectionViewController!
So use this initializer instead: 因此,请改用以下初始化程序:
let homecon = HomeController(collectionViewLayout: UICollectionViewLayout())
Oh and it's just the start of the week here. 哦,这只是本周的开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.