1

I have a collectionView cell that displays the cells horizontally. I only want a set number of cells to show on the screen and after the amount I want to display ... or something to signify there are more cells available. I don't want the user to scroll to more cells

Because there are different screen sizes like iPhone 4/5/6/7/iPad etc.. I don't want to put an exact number of items to get displayed. Instead I want to to truncate the amount of items based on the screen size for eg. On an iPhone 7+ in .compact size class at least five cells can fit on the screen but on an iPhone 4 only three cells can fit on a screen and on an iPad probably 20 cells.

I imagine I'd do something like this:

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

    //I know this is comparing an Int to a CGRect and it won't work
    if items.count > UIScreen.main.bounds.width{
         //stop count and display truncation dots ...
    }
    return self.items.count
}

How would I accomplish this?

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

1

To accomplish that you need to implement the protocol method

collectionView(_ collectionView: UICollectionView, 
                  layout collectionViewLayout: UICollectionViewLayout, 
           sizeForItemAt indexPath: IndexPath) -> CGSize

You need to manually return the size of each cell so in this case you will calculate the width of the cell depending on your screen size.

Jonathanff
  • 409
  • 3
  • 12
  • thanks I'll try it. How would I display the truncation dots ...? I was thinking maybe I have a specific cell t,with the dots already inside them and when the cells get close to the screen's bounds the truncated cell gets displayed last. Unless there is a better way? – Lance Samaria Jun 25 '17 at 15:44
  • Adding a comment to my response you will need to calculate in which screen you're to return the number of items in section you want depending on the screen and then add a last row the display the dots. – Jonathanff Jun 25 '17 at 15:48
  • Thanks I'll try it – Lance Samaria Jun 25 '17 at 15:50