0

There are a few posts on UICollectionViews and as a general concept I understand UICollectionViews are similar to how TableViews are implemented. There are a few posts on this topic already.

How to make a simple collection view with Swift

I'm using a UICollectionView to layout 8 cells for essentially decorative purposes...just as a container to hold a few images for a page. The images aren't meant to be interactive eg. a shopping cart.

My question is whether I need to implement the function below (and other similar functions) if i'm laying out my cells on the storyboard.

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

I have 8 cells within a UICollectionView that i have configured on the storyboard (this collection view is a view on a UIViewController) and each UICollectionViewCell has an image view with in. I have laid this all out on the storyboard.

I also don't want to put an image in each cell. Just a certain number of cels eg. cells 2,3,5 and 8.

So what I was going to do is create a class that extends UIControllerViewCell and create an IBOutlet for the image view and then in the interface builder reference the custom cell class.

And I think - but could do with some advice - then all I need to do is use the function below and because I only want to populate certain cells I can use an if statement for index path when it's 2,3,5 and 8:

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

  if(indexPath == 2 || 3 || 5|| 8)
  {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.image = self.items[indexPath.item]


        return cell
  }
}

Thanks.

Amit
  • 4,157
  • 4
  • 25
  • 40
Mike
  • 1,151
  • 1
  • 12
  • 33

2 Answers2

1

Sounds like you are asking about a static UICollectionViewController which is not possible. You will make a single prototype cell in the storyboard and link the IBOutlets with the desired class. You will also need to implement the data source and delegate methods for UICollectionView. Just hide the image where you don't want to show it based on the index.

  • Hi. Thanks Hassan. Can I ask then why does the UICollectionView let me arrange multiple cells into multiple rows on the storyboard? For example I want two rows the 4 cells in each row. I can set this up on the storyboard but you're saying that I really just need one cell and then programmatically create 8 cells? Using the UITableView as an example there is obviously only one prototype cell and then programmatically you specify the number of rows. – Mike Oct 03 '17 at 07:04
  • You can set as many cells in the storyboard as you want. But they wont show in the app unless you implement delegate methods of UICollectionview. You can place multiple cells in the storyboard to view the correct width and height of cells or if you want to have multiple type of cells in the collectionview you can design them in the storyboard. Same is the case with UITableview. UITableview can have more than one prototype cell. – Hassan Javed Oct 05 '17 at 04:39
1

cellForItemAt must return value (which is UICollectionViewCell instance in this case), but you are free to set image of certain cells to nil (AKA no image)

eg:

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionViewCell

    if([2,3,5,8].contains(indexPath.row))
    {
        // Use the outlet in our custom class to get a reference to the UIImageView in the cell
        cell.myImageView.image = self.items[indexPath.row]
    } else {
        cell.myImageView.image = nil
    }

    return cell
}
zgorawski
  • 2,327
  • 4
  • 25
  • 41