-3

I am initializing a UICollectionView as below , I want different line spacing for different section.

 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
 layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

 var baseCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
 baseCollectionView.backgroundColor = UIColor.clearColor()
 baseCollectionView.translatesAutoresizingMaskIntoConstraints = false
 baseCollectionView.showsVerticalScrollIndicator = false
 baseCollectionView.delegate = self
 baseCollectionView.dataSource = self

And then defining the layout methods as follows ,

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
    switch section  {
    case 0:
        return 10.0
    case 1:
        return 10.0
    case 2 :
        return 10.0
    case 3:
        return 10.0
    case 4 :
        return 0.0
    case 5 :
        return 0.0
    case 6 :
        return 0.0
    case 7 :
        return 0.0
    default:
        return 0.0
    }
}

And,

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
{
  switch section  {
    case 0:
        return 10.0
    case 1:
        return 10.0
    case 2 :
        return 10.0
    case 3:
        return 10.0
    case 4 :
        return 0.0
    case 5 :
        return 0.0
    case 6 :
        return 0.0
    case 7 :
        return 0.0
    default:
        return 0.0
    }
}

And other delegates too, minimumInteritemSpacingForSectionAtIndex is called but minimumLineSpacingForSectionAt delegate method is not getting called , how to solve this , where am I wrong?

I have conformed to delegate protocols too.

class MyViewController: UIViewController , UICollectionViewDelegateFlowLayout , UICollectionViewDelegate , UICollectionViewDataSource

Here you go,

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    switch section {
    case 0:
        return CGSizeMake(140.0,self.contentView.frame.size.height)
    case 1:
        return CGSizeMake(140.0,self.contentView.frame.size.height)
    case 2 :
        return CGSizeMake(140.0,self.contentView.frame.size.height)
    case 3 :
        return CGSizeMake(140.0,self.contentView.frame.size.height)
    case 4:
        return CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)
    case 5:
        return CGSizeMake(self.contentView.frame.size.width / 2.0,self.contentView.frame.size.height)
    case 6:
        return CGSizeMake(75.0, self.contentView.frame.size.height)
    case 7:
        return CGSizeMake(((self.contentView.frame.size.width - 40.0) / 3.0), 150.0)
    default:
        return CGSizeZero

    }
}

For all the other people, actually the display design sent from my server is being used to set the various constants.

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • it is hard to believe that is a _live_ code... syntactically it does not seem compilable at all. – holex Nov 30 '17 at 10:38
  • What makes you feel that? And I will only post parts of code I feel is necessary for getting my answer , not my whole code base for you! – Avinash Sharma Nov 30 '17 at 10:40
  • 2
    @AvinashSharma that last comment is entirely uncalled for and not helpful in any way. FYI, you can group cases together if they return then same thing. And you can pattern match also... `case 0...3: return 10` ad `default: return 0` – Fogmeister Nov 30 '17 at 10:42
  • I have written that I have added all the needed methods, If I do not implement that method my cells won't appear at all – Avinash Sharma Nov 30 '17 at 10:43
  • @Fogmeister I have tweaked my code to post it on stack overflow, as I was using variables name and other pieces of logic in there , which would be a problem if posted here, I am not asking to better my code, but just wondering why one method is not called? – Avinash Sharma Nov 30 '17 at 10:45
  • 1
    @AvinashSharma, maybe there is not enough cell in your collection-view and that is why it does not need to invoke the [`...minimumLineSpacingForSectionAt:...`](https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617705-collectionview) method as if you don't have enough cell for at least 2 lines, the line-spacing is irrelevant, therefore the system does not care about invoking that. – holex Nov 30 '17 at 10:51
  • Also, you should not be using `CGRectZero` and `CGSizeMake` and `CGSizeZero` these will both be removed in Swift 4 and since Swift 2 have been replaced with `CGRect.zero` and `CGSize(width: CGFloat, height: CGFloat)` and `CGSize.zero` – Fogmeister Nov 30 '17 at 10:53

3 Answers3

1

The function name does not have Index in the signature.

From the docs https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout

The function signature is ...

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat

i.e. minimumInteritemSpacingForSectionAt

not minimumInteritemSpacingForSectionAtIndex

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    Thanks for pointing this out, although my original question was that `minimumLineSpacingForSectionAt ` is not getting called, I changed it to `minimumLineSpacingForSectionAtIndex` and it works, as I am using Swift 2.3 and not 3.0 so the mistake was in syntax. Your answer helped me to look into it so thanks – Avinash Sharma Nov 30 '17 at 12:27
1

your code is working, change like this

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

Have you done this?

@interface YourViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
Nagarjun
  • 6,557
  • 5
  • 33
  • 51