genaks genaks - 5 months ago 18
Objective-C Question

Number of sections in a collection view

I have two

UICollectionViews
in a particular
UIViewController
and I wish to have a different number of sections in the two of them. I have been trying this in the following manner

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView == self.firstcollectionView) {
return objects.count + 1;
}
else
{
return 1;
}
}


But it does not seem to work for some reason.

Also tried it with tags but haven't been able to make it work that way either.

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView.tag == 1) {
return objects.count + 1;
}
else
{
return 1;
}
}


It always shows a single section in both the
UICollectionViews
when I have been able to check that there is 1 object which should mean that there will be 2 sections in the first
UICollectionView


UPDATE 1 : On debugging it seems the control never goes into the if-case returning 1 always.

UPDATE 2 : The if condition is where the problem is. It never gets satisfied.

UPDATE 3 : I was able to get the if-else working by declaring the
UICollectionViews
as a class variable rather than as a property. The number of sections is still 1 for both the
UICollectionViews
though.

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView == cardscollectionView) {
return 2;
}
else
{
return 1;
}
}


Still didn't work. While this

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 2;
}


worked just fine. Just to illustrate the layouts are fine and 2 cells do show up in both the collection views.

SAMPLE PROJECT (On huge demand :D) -

https://github.com/genaks/UIICollectionView-Sections

I have not yet gone to the extent of reloading the UICollectionView as the current setup hasn't been working altogether

Answer

Make sure that you have set delegate and data source to self for both collection views like

   self.firstcollectionView.delegate = self;
   self.firstcollectionView.datasource = self;

And same for second collectionview also.

And make sure that you have connected your outlet properly.

or

Make sure that you are setting tag properly

Update:

 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {

if (collectionView == self.cardscollectionView) {

    NSLog(@"getting called. It is cardcollerctionView");
    return 2;

}
if (collectionView == self.bankscollectionView) {
    NSLog(@"getting called. it is bankcollectionView");
    return 1;
}

else
{
    return 1;


}

    //use above or below both are working fine.
    //******************************* OR ********************************************

    if (collectionView == self.cardscollectionView) {

        NSLog(@"getting called. It is cardcollerctionView");
        return 2;

    }
    else
    {
        NSLog(@"it is else");
        return 1;


    }

 }

I have run your demo and it's work fine. I have updated answer with numberofsection method. both portion of code is working. Make sure that you are not using same UICollectionViewLayout for both collectionViews

Hope this will help :)

Comments