I have a UICollectionView
and Im trying to set a label
and an image
in the collectionViewCell
. Unfortunately I cant seem
You are missing the:
[cell.contentView addSubview:issue];
Clear example to use custom collectionViewCell
.
Create a separate class subclass ofUICollectionViewCell
see below code:
.h file:
#import <UIKit/UIKit.h>
@interface CollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *customLabel;
@end
.m file:
#import "CollectionViewCell.h"
@implementation CollectionViewCell
@end
Now drag and drop the collectionView
inside viewController
using storyboard
then by selecting cell set custom class for it and connect its IBOutlet
of label see below image.
Setting up custom class:
Connecting label's outlet: if adding label and other ui component from storyboard
Note: Drag uilabel
inside cell before you connect its IBOutlet
.
Now configure cell inside your viewController
class. And configure collectionView
correctly by connecting its delegate
, dataSuorce
and IBOutlet
.
#import "ViewController.h"
#import "CollectionViewCell.h"
@interface ViewController (){
// instance variable deceleration part
NSMutableArray *yourArray;
}
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
_yourCollView.delegate = self;
_yourCollView.dataSource = self;
yourArray = [[NSMutableArray alloc] initWithObjects:@"1st cell",@"2nd cell",@"3rd cell",@"4th cell", nil];
// Do any additional setup after loading the view, typically from a nib.
}
// collection view delegate methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [yourArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustomCell" forIndexPath:indexPath];
// configuring cell
// cell.customLabel.text = [yourArray objectAtIndex:indexPath.row]; // comment this line if you do not want add label from storyboard
// if you need to add label and other ui component programmatically
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
label.tag = 200;
label.text = [yourArray objectAtIndex:indexPath.row];
// this adds the label inside cell
[cell.contentView addSubview:label];
return cell;
}
//Note: Above two "numberOfItemsInSection" & "cellForItemAtIndexPath" methods are required.
// this method overrides the changes you have made to inc or dec the size of cell using storyboard.
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100);
}
} // class ends
Setup the cell identifier (by selecting a cell) MyCustomCell
you have given inside cellForItemAtIndexPath
method before use see below image:
Note: Change the text color of uilabel
to white before because by default collectionView
appears black.
Hope here you understand.
UICollectionViewCell
. For instance TestCollectionViewCell
.Storyboard
drag label
in cell and set "Custom class"
for this UICollectionViewCell
with your created class. Set Reusable identifier
, if your collection view in UIViewController
don't forget to set DataSource
and Delegate
for that collectionView
.IBOutlet
in your Cell subclass.numberOfItemsInSection
method.cellForItemAt
and try set text for a label.