marciokoko
marciokoko

Reputation: 4986

How to set a UILabel in UICollectionViewCell

I have an app with a UIViewController with a UICollectionView IBOutlet in it.

MyCustomCell Outlet connected MyCustomCell ReuseID MyCustomCell Class Identity

The cell in the UICollectionView is MyCustomCell and has this method setting its UILabel:

-(void)setCellLabel:(NSString *)value{
    NSLog(@"settinglabel");
    cellLabel.text = @"hardcode"; //added for testing purposes
}

The cell has the property Class type identifying it as MyCustomCell in storyboard and its dequeue identifier as well. The UIViewController adopts the datasource and delegate protocols. The IBOutlet UILabel has its cellLabel outlet connected to the storyboard. The methods are defined as:

- (void)viewDidLoad{
    [super viewDidLoad];
    self.restNames = @[@"Orange",@"Naranja",@"Narnia"];

    [self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"MyCustomCellID"];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"%d",[self.restNames count]);
    return [self.restNames count];
}

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

    MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];


    [cell setCellLabel:@"hi"];
    NSLog(@"fitsname %@",[self.restNames objectAtIndex:indexPath.row]);


    return cell;
}

I get three cells drawn in my tableview, corresponding to the 3 objects in my array. The array simply contains string objects which I was setting directly by objectAtIndexPath but I decided to set it directly to @"hi" since it wasn't working. I even changed the value used in setCellLabel method to a hardcoded value and I keep get just the "Label" default string in each cell.

Why isnt the cellLabel being set properly?

Upvotes: 3

Views: 13565

Answers (3)

Dulgan
Dulgan

Reputation: 6694

When you're using Storyboard, the default template is wrong, because it uses the line

[self.collectionView registerClass:[UICollectionView class] forCellWithReuseIdentifier:CellIdentifier];

But the storyboard already registered it. So basically this is what you have to do to create a custom, storyboard based UICollectionView :

  1. Create your UICollectionViewController subclass
  2. Click&drag a UICollectionViewController to the storyboard and change the configuration as you want
  3. Create a UICollectionViewCell subclass
  4. Set the cell class in the storyboard, set it's reuse identifier ctrl-drag the needed outlets
  5. Remove the [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: CellIdentifier];
  6. Set the static reuse identifier in the UICollectionView subclass

Upvotes: 5

Patrick Tescher
Patrick Tescher

Reputation: 3447

Do you have your cellLabel outlet set in interface builder?

You should have something like this in your cell's .h file:

@property (weak, nonatomic) IBOutlet UILabel *cellLabel;

then later you really just need to do this:

cell.cellLabel.text = @"";

Upvotes: 5

tiguero
tiguero

Reputation: 11537

You have probably forgotten to tell your UICollectionView what class it’s supposed to use to create cells. This is done with the following line of code:

[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier: CellIdentifier];

You can set this at the end of the viewDidLoad of your controller.

Upvotes: 0

Related Questions