Reputation: 4986
I have an app with a UIViewController with a UICollectionView IBOutlet in it.
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
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
:
UICollectionViewController
subclassUICollectionViewController
to the storyboard and change the configuration as you wantUICollectionViewCell
subclass[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: CellIdentifier];
UICollectionView
subclassUpvotes: 5
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
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