【问题标题】:CollectionView controller creation without storyboard没有情节提要的 CollectionView 控制器创建
【发布时间】:2016-03-09 11:53:15
【问题描述】:

大家好,我是 Swift 新手,目前遇到以下问题。 当在表视图控制器上选择一行时,它会推送一个集合视图控制器,但我收到此错误:

UICollectionView must be initialized with a non-nil layout parameter.

在 tableview 控制器上选择行时出现错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

请指教一下?

应用代理:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController = CustomTabBarController()

    return true
}

选项卡控制器已在选择行时使用表视图控制器进行初始化,它应该按到Collection View Controller

collection view cell 已经注册到viewDidLoad

collectionView?.registerClass(PostCell.self, forCellWithReuseIdentifier: CELLID)

【问题讨论】:

  • class PostCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder: ) 尚未实现") } func setupViews(){ backgroundColor = UIColor.greenColor() }
  • 我正在尝试在没有情节提要的情况下使用它。因此不确定如何在应用委托上初始化 collectionview 控制器
  • 显示你的 cellForItemAtIndexPath
  • 让单元格 = collectionView.dequeueReusableCellWithReuseIdentifier(CELLID, forIndexPath: indexPath) 返回单元格

标签: ios swift controller collectionview


【解决方案1】:

终于解决了。在 Collection 视图控制器中使用以下方法初始化

**override init(collectionViewLayout layout: UICollectionViewLayout) {
    super.init(collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}**

在推动视图控制器的同时 让 flowLayout = UICollectionViewFlowLayout() 让 newPostCollection = TestCVC(collectionViewLayout: flowLayout)

谢谢大家

【讨论】:

    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多