周末愉快。我在 didSelectItemAt indexPath 函数下遇到问题,我想要使用新的 View Controller ,它显示详细信息,并且每个单元格必须具有不同的 View Controller (它将根据来自协议(protocol)的数据进行更改)。我尝试了很多方法来做到这一点,但我无法弄清楚。我调用了当前和推送 View Controller 代码,但它也不起作用,因为 navigationController 代码无法在 didSelectItemAt 函数下工作。最后我尝试了:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let homecon = HomeController()
homecon.showDetailPage() }
func showDetailPage() {
let newView = pageDetail()
self.present(newView, animated: true, completion: nil)
}
(HomeController 中的 showDetailPage 函数)。但该代码也给出了错误,即
'UICollectionView must be initialized with a non-nil layout parameter'.
所以我无法解决这个问题。我该如何解决它请帮助我。祝你工作愉快!
最佳答案
这是因为 HomeController
是 UICollectionViewController
的子类,如果没有布局,collectionViewController 就无法初始化!
因此请改用此初始化程序:
let homecon = HomeController(collectionViewLayout: UICollectionViewLayout())
哦,这只是本周的开始。
关于swift - 如何在didSelectItemAt indexPath函数下显示ViewController? (没有 Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647758/