How to set UILabel in UICollectionViewCell

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

MyCustomCell Outlet connectedMyCustomCell ReuseIDMyCustomCell Class Identity

The cell in the UICollectionView is MyCustomCell and this method sets the UILabel:

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

A cell has a class property that identifies it as MyCustomCell in the storyboard and its detection identifier. UIViewController accepts data and delegation protocols. IBUutlet UILabel has a CellLabel output connected to the storyboard. 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 table view, corresponding to 3 objects in my array. The array simply contains the string objects that I installed directly with objectAtIndexPath, but I decided to set it directly to "hi" since it did not work. I even changed the value used in the setCellLabel method to a solid value, and I save only the default label string in each cell.

Why is the cell cell not set correctly?

+2
ios uicollectionviewcell
source share
3 answers

Do you have a cellLabel socket installed in the interface constructor?

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

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

then later you just need to do this:

 cell.cellLabel.text = @""; 
+5
source share

When you use a Storyboard, the default template is erroneous because it uses a string

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

But the storyboard had already registered him. So basically this is what you need to do to create a custom storyboard based on the UICollectionView :

  • Subclass UICollectionViewController
  • Click & drag the UICollectionViewController into the storyboard and change the configuration as you like.
  • Subclass UICollectionViewCell
  • Set cell class in storyboard, set reuse identifier ctrl-drag desired outputs
  • Remove [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: CellIdentifier];
  • Set static reuse identifier in subclass of UICollectionView
+5
source share

You may have forgotten to tell your UICollectionView which class it should 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.

0
source share

All Articles