Kanan Jarrus
Kanan Jarrus

Reputation: 617

CollectionView controller creation without storyboard

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)

Upvotes: 0

Views: 933

Answers (1)

Kanan Jarrus
Kanan Jarrus

Reputation: 617

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

Upvotes: 2

Related Questions