Pes
Pes

Reputation: 23

How to automatically group items in sections in UICollectionView?

I have a fixed number of Sections in a UICollectionView

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

return 3;

}   

and a fixed number of items per section

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 9;

}

The first 9 items are properly displayed through a plist (with cellForItemAtIndexPath) in the first section. However the second and third sections only display the items from the first section, instead of displaying the required items.

So, I'm trying to display all the items from the plist, and have them grouped automatically into sections (of 9 items).

I'm fairly new to Objective-C and have been struggling with this for quite a while, so I'd really appreciate your help.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


        xx.image = [UIImage imageNamed:[imagePath objectAtIndex:indexPath.item]];


    }

Upvotes: 2

Views: 1769

Answers (2)

Manthan
Manthan

Reputation: 3914

You can try this way.

You need to specify indexpath this way.

 xx.image = [UIImage imageNamed:[imagePath objectAtIndex:indexPath.section * number of sections + indexpath.row]];

Hope this helps you.

Upvotes: 1

czechboy
czechboy

Reputation: 944

Judging by "the second and third sections only display the items from the first section" I would think you are incorrectly identifying the section number (maybe ignoring it altogether?) in your cellForItemAtIndexPath. Please add this method to the question, because the problem is most likely there.

Upvotes: 0

Related Questions