swift - 如何在didSelectItemAt indexPath函数下显示ViewController? (没有 Storyboard)

标签 swift xcode uicollectionview uicollectionviewcell

周末愉快。我在 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'.

所以我无法解决这个问题。我该如何解决它请帮助我。祝你工作愉快!

最佳答案

这是因为 HomeControllerUICollectionViewController 的子类,如果没有布局,collectionViewController 就无法初始化!

因此请改用此初始化程序:

let homecon = HomeController(collectionViewLayout: UICollectionViewLayout())

哦,这只是本周的开始。

关于swift - 如何在didSelectItemAt indexPath函数下显示ViewController? (没有 Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647758/

相关文章:

ios - AKMicrophone 导致 SIGABRT 错误 - 一个错误?

ios - 特定缩放取决于注释

ios - 合并两个 NSFetchedResultsController

ios - Swift UITextField 约束 - 在 View 中间添加文本字段

xcode - 将窗口显示为模式的问题

ios - 在更改单元格上重置 UICollectionView

ios - 从 subview 调用 UIView.animate 不起作用

c++ - 编译器生成的无效 DWARF

ios - 现有 CollectionView 图像在滚动时发生变化

ios - 在 UICollectionViewCell 中将 CAGradientLayer 添加到 UIImageView 有奇怪的行为