0

Hi all I am new to Swift and am currently stuck on the below problem. When a row is selected on a table view controller it pushes a collection view controller but I am getting this error:

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

I get the error when the row is selected on tableview controller

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

Any pointer on this please?

App Delegate:

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

    window?.rootViewController = CustomTabBarController()

    return true
}

Tab bar controller has been initialised with table view controller when the row is selected it should push to collection view controller

collection view cell has been registered in viewDidLoad:

collectionView?.registerClass(PostCell.self, forCellWithReuseIdentifier: CELLID)
Eendje
  • 8,815
  • 1
  • 29
  • 31
Kanan Jarrus
  • 607
  • 1
  • 12
  • 26
  • class PostCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupViews(){ backgroundColor = UIColor.greenColor() } – Kanan Jarrus Mar 09 '16 at 11:53
  • http://stackoverflow.com/questions/24288927/uicollectionview-must-be-initialized-with-a-non-nil-layout-parameter – Awesome.Apple Mar 09 '16 at 11:55
  • i am trying to use it without storyboard. hence not sure how to initialise collectionview controller on app delegate – Kanan Jarrus Mar 09 '16 at 12:02
  • Show your cellForItemAtIndexPath – Bharat Modi Mar 09 '16 at 12:26
  • let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELLID, forIndexPath: indexPath) return cell – Kanan Jarrus Mar 09 '16 at 12:32
  • Provide the code where you are creating collectionView – Bharat Modi Mar 09 '16 at 12:34
  • class TestCVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout) collectionView?.registerClass(PostCell.self, forCellWithReuseIdentifier: CELLID) /***** ****/ } – Kanan Jarrus Mar 09 '16 at 12:39

1 Answers1

2

Finally resolved it. In Collection view controller initialize with below method

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

And while pushing the view controller let flowLayout = UICollectionViewFlowLayout() let newPostCollection = TestCVC(collectionViewLayout: flowLayout)

Thank you all

Kanan Jarrus
  • 607
  • 1
  • 12
  • 26