Collection View not showing up

I'm currently making a tabbed app, and im having some troubles getting some things to show up during the test runs. How do you get the collection view that was made in a storyboard to show up when running the test on a device? everything is configured porperly in the storyboard, and other things show up in other tabs, like the navigation bars and such, but for some reason, i can't get the collection view to appear

  • Select the UICollectionView in the View Controller Scene menu (middle of Xcode, but left of Main Storyboard visual), and then in Size Inspector change the "Estimated Size" from AUTOMATIC to "NONE". If you have all your hookups done correctly, and constraints are correct in your main Storyboard scene, you should see approximately what you designed instead of small cells.

Add a Comment

Replies

Have you defined the delegate and datasource for the collectionView ? That's the most likely cause for not showing any content of collectionView.


However, the collectionView background should show.

You should set the color of collectionView to check that you see it.


If not, you may have constrai,nts thjat are not correct.

What should i be defining the delegate and datasource as? Everytime i set the delegate and datasource to the view controller, and i run the test, the moment i tap on that tab with the collectionview, it crashes the app and xcode gives me an error

Which error do you get ?


How do you set the delegates: in IB ? in code ?

The simplest is to set them in IB, so you are sure to connect to the right controller.


Have you declared your viewController as conforming to collectionView protocols ?


classMyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

when i connect the datasource and delegate through the IB, and tap on the tab, i get the error "libc++abi.dylib: terminating with uncaught exception of type NSException" and then it opens the AppDelegate.swift and highlights the first line with the error "Thread 1: signal SIGABRT." and if i declare the collectionview protocols, i am clueless what to put in the code for the indexpath...if i just do the delegate through the IB, and run the app, i have no issues, its specifically the datasource connection that makes the app crash.

In some cases, dataSource is implicitely defined (that's why it may work even though you did not connect).


when i connect the datasource and delegate through the IB,


Please detail where are (in which classes)

- collectionView

- the controller you connect for dataSource and delegate.


Please, also post the complete code for the controller that includes the collectionView


Note: the fact that crash appears in AppDelegate isdoes not mean error occurs here.

Heres the code for the second view controller:

import UIKit

class SecondViewController: UIViewController {

     override func viewDidLoad() {
          super.viewDidLoad()
     
     }

}

i have on the actual scene in the IB, the collection view, the collection view cells, and a segmented control. The segmented control shows up in the tests and i have no issues with it. and when i test the collection view by changing the background color, it shows up. It's just the cells that i cant get to show up.


When i connect the datasource and delegate, im connecting the collection view to the scene that it's in, which is the SecondViewController. It is specifically the datasource connection that causes problems with the app when i tap on the tab.

Sorry, but that code shows nothing useful to understand what's going on.


i have on the actual scene in the IB, the collection view, the collection view cells, and a segmented control.

Are they all in one controller. Is it SecondViewController ? Or another one ?


The segmented control shows up in the tests and i have no issues with it.

and when i test the collection view by changing the background color, it shows up.


Where do you see ? In IB or in the running app ?


It's just the cells that i cant get to show up.

You cannot show them when running the app or in IB ?


What is needed is the code of the viewController that includes the collectionView.


If you cannot show the full code (not just viewDidLoad), sorry, impossible to help you.

They are all in one controller. Is it SecondViewController ? Or another one ?

Everything is in the SecondViewController.


Where do you see ? In IB or in the running app ?

When i run the test, i run it through my iPhone X, and thats where the segmented control shows up; on both the storyboard and within the test run of the actual app.


You cannot show them when running the app or in IB ?

The collection view cells show up in the IB, but not when running the app.


If you cannot show the full code (not just viewDidLoad), sorry, impossible to help you.

That code i wrote is all of the secondviewcontroller code. Theres nothing else written in there. I am unsure of what to write to get the cells to show up. Everything else is showing up without me having to add any extra code into the SecondViewController

So, to see the cells, you need to declare that class conforms to collectionView delegate protocols and write the collectionView delegate methods. Should be something like this:


You should define a class (FirstCollectionViewCell) for the collectionViewCells, with its xib (create a new file, select it as subclass ou UICollectionViewCell and create the xib at the same time)


import UIKit

class FirstCollectionViewCell: UICollectionViewCell {
  
    @IBOutlet weak var label: UILabel!
  
    override func awakeFromNib() {
      
        super.awakeFromNib()
        // Initialization code
    }
  
}


In IB, select the CollectionView, select the cell and hive it the class FirstCollectionViewCell


Create a label in its xib that you connect to the IBOutlet



import UIKit

class SecondViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

     var items : String = []          // Just to have a sample data

    @IBOutlet weak var collectionView: UICollectionView!     // connect to the CollectionView in IB ; you may need it later

     override func viewDidLoad() {
          super.viewDidLoad()
          // Let's create the dataSource example
          for i in 1...10 {
               let text = "MyCell n° \(i)"
          }
         collectionView.delegate = self // Unless you have already defined the delegate in IB
         collectionView.dataSource = self // Unless you have already defined the dataSource in IB
     }

     // Here are the delegate and dataSource methods

     // If you have only onse section, you can omit this one, but it is good practice to include
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return items.count  // howMany cells do you have in collection ? that is items.count, where items is the array containing the cells data
    }

// Here, you populate the collection from the dataSource
// You need to have defined a cellIdentitifier in IB : I call it CollectionCellID
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellID", for: indexPath) as! FirstCollectionViewCell
        cell.label.text = items[indexPath.row]
        return cell
    }

}

Hope I did not forget anything

im receiving numerous errors after typing in the code, specifically in the lines:


var items : String = []

"cannot convert valueof type '[any]' to specified type 'String'


collectionView.delegate = self
collectionView.dataSource = self

both receive the error: Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)'


let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellID", for: indexPath) as! FirstCollectionViewCell
cell.label.text = items[indexPath.row]

and for those 2 lines it's the error: subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion

var items : String = []


of course, should be


var items : [String] = []


And just remove


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

return 1

}

Can you post the exact code of your SecondViewController, and report any error after the modfication.

alright, after waiting for Apple to release an update so i could actually open my app, i finally got something to show up from the collection view. The only problem that im having is the size of the cell, and im unsure of how to get the other cells to show up. here's my working code:

import UIKit

class SecondViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1",  for: indexPath)
        return cell
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
}

Did you define the cell size in IB or in code ?

IB. but i also have 6 cells, each with a different image in it. its showing 6 cells in 2 columns, exactly like i want it to, but its all the same cell, just repeated, and very small