How to set a UILabel in UICollectionViewCell

末鹿安然 提交于 2019-12-19 09:42:30

问题


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?


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/17957819/how-to-set-a-uilabel-in-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!