Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aaron Watkins
10,902 PointsMultiple UICollectionViewCells in single UICollectionView
Hi,
I'm looking to add another custom UICollectionVIewCell to my UICollectionView. So I would now have two custom cells in the same collectionView. All of the delegate methods I have put in are for the first collectionViewCell (collectionVIewCellA). I've added collectionViewCellB to my storyboard, but am unsure of how to tell the viewController about this new custom cell and to configure it. Has anyone done this? Thank you so much for your help.
3 Answers
Aaron Watkins
10,902 PointsThanks, John. I added Cell2 to the cellForItemAtIndexPath method with a different CellIdentifier, but the cell still doesn't appear. Here's a bit of what I am working with.
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.carsArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
DetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
HeaderCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier2 forIndexPath:indexPath];
cell2.self.nameLabel.text = @"aaron";
//more code that configures DetailCell
}
DetailCell shows up with no problem, which is populated by the information in carsArray. But HeaderCell won't appear. In my storyboard I've also named the Reuse Identifier "Cell2" and gave it a class of HeaderCell. HeaderCell.h has a label called nameLabel. I know I'm probably not doing something really simple, so forgive me. Thanks for your help.
John W
21,558 PointsAs in UITableView, UICollectionView has the following method in its datasource protocol.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Simply implement it and call [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier] with a different cellIdentifier as set in the storyboard depending on your indexPath.
Aaron Watkins
10,902 PointsAh! I got it to work (not sure if it's the absolute right way to do it, but in case someone else wants to know...). What I wasn't doing is including an if statement. so something like
if (indexPath.item == 0){
//do all the stuff here for CellA
}else{
//CellB
}
Thanks again for your help.
John W
21,558 PointsYup, that's the way to do it. You dequeue cell identifier "Cell1" when indexPath.item==0 and dequeue "Cell2" for indexPath.item == anything else.