0

I have a collection view and I want some cells to display even if there is a small amount of data. For example, in this picture, notice how there is only 1 item but all the other cells are still showing with a background color.

collectionView

I know how to do this only by faking the amount of data used to populate the collection view in numberOfItemsInSection. This is my sample code:

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    let itemArray = ["1"]
}

extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        /// Note I can fake more cells here but don't think this is correct way.
        return itemArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FavoritesCollectionViewCell

        return cell
    }
}

This only needs to work until there is enough cells to display in a screen. For example, there needs to be about 8 cells to fill the screen, once there are enough items in the array to fit the screen, this is no longer necessary.

Any thoughts?

Update 1:

This collection view will support adding ,deleting, and sorting the items. Not sure if this will this be a problem if I add fake data in numberOfItemsInSection

JEL
  • 1,540
  • 4
  • 23
  • 51

2 Answers2

0

Note I can fake more cells here but don't think this is correct way

Why not? If you want empty cells, ask for empty cells! It isn't "faking" at all.

The alternative would be to draw (in real time) a grid that looks like your empty grid and display it as the backdrop of the collection view.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I updated my question, please let me know if it would be an issue - should've have mentioned it before – JEL Jan 30 '17 at 23:58
  • Well then what about my other idea? You can provide the empty cells as "decoration views", or just as a background drawing. – matt Jan 31 '17 at 00:22
  • I think thats really the only way to go then - thanks! – JEL Feb 02 '17 at 00:51
  • Hello matt, I have an AVFoundation question with a bounty, it seems from your books you are like an expert in this, could you please take a look: http://stackoverflow.com/questions/43282423/play-pause-button-not-updating-in-ios-command-center – JEL Apr 11 '17 at 00:02
0

I would just do something like:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return max(itemArray.count, 8)
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FavoritesCollectionViewCell

    if (indexPath.row <= itemArray.count) {
        // just guessing here how your setting image
        cell.image = itemArray[indexPath.row].image
    }

    return cell
}

I think you're on the right track, its not faking - you're just setting up the cell and setting a min number of elements

pflous
  • 573
  • 6
  • 17