-2

After using my old way of using collectionView I'm using now Apple code snippet for collectionView. The problem is that I can't implement my code for Cells spacing and that exact 3 cells for example should be in a row.

Here's my old code for cell spacing (now it's not working idk why..)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.width / 3 - 1 // number of cells in a row. now it's 3 

    return CGSize(width: width, height: width)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

How I could make everything look better than it's now? Because now cell number in a row depends on iPhone model. Also, spacing depends on iPhone model too.

SwiftyLifestyle
  • 416
  • 5
  • 17
  • Please include only _relevant_ code in your question. Don't dump whole classes and make people read through code that has nothing to do with what you're asking. Please read [mcve] and [edit] your question with the _minimum_ code that demonstrates the problem – Ashley Mills Oct 21 '18 at 13:46
  • @AshleyMills I said that I don't know how Apple is dealing with spacing between cells that's why I put all of my code here. Now I changed the code to the hyperlink. I'm not a professional in Swift and if I would know which part of the code is wrong I probably would fix it by myself. – SwiftyLifestyle Oct 21 '18 at 14:01
  • 1
    I think even to a non-professional developer, it would be clear that code to fetch from a photo library, or to cache assets, doesn't affect the collection view layout. Creating a [mcve] isn't just for our benefit, by removing code that is irrelevant, it will help you see where the problem lies. Please add the code that is to do with collection view layout, not all the other stuff – Ashley Mills Oct 21 '18 at 14:03
  • @swiftylifestyle : You will have to subclass flow layout and override `layoutAttributesForElementsInRect` to achieve what you want, there are plenty of tutorials online explaining the exact same solution, so please look for subclassing flow layout tutorial you will find one – Sandeep Bhandari Oct 21 '18 at 17:03
  • 1
    @AshleyMills Actually, you're strict but right :) Anyway, I found the solution so I posted my answer which is perfect. Also, I deleted the code I was using because it was unrelated. Sorry for that. – SwiftyLifestyle Oct 21 '18 at 21:45

1 Answers1

0

In order you want to set spacing or how many of cells you want to display all you need to do is use UICollectionViewDelegateFlowLayout.

Here's the perfect code for that:

extension CollectionViewController: UICollectionViewDelegateFlowLayout {


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.width / 3 - 1

    return CGSize(width: width, height: width)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

Also, write collectionView?.delegate = self in your viewDidLoad function.

SwiftyLifestyle
  • 416
  • 5
  • 17