简体   繁体   中英

How to set a UILabel in UICollectionViewCell

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

MyCustomCell Outlet已连接MyCustomCell ReuseIDMyCustomCell类标识

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?

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 = @"";

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM