如何在UICollectionViewCell中设置UILabel [英] How to set a UILabel in UICollectionViewCell

查看:530
本文介绍了如何在UICollectionViewCell中设置UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIViewController的应用程序,其中包含UICollectionView IBOutlet。







UICollectionView中的单元格是MyCustomCell并且具有此方法设置其UILabel:

   - (void)setCellLabel:(NSString *)value {
NSLog(@ settinglabel);
cellLabel.text = @hardcode; //为了测试目的而添加
}

单元格具有属性Class类型,将其标识为故事板中的MyCustomCell及其出列标识符。 UIViewController采用数据源和委托协议。 IBOutlet UILabel的cellBabel插座连接到故事板。方法定义为:

   - (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]) ;
返回[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]);


返回单元格;
}

我在tableview中绘制了三个单元格,对应于我的3个对象阵列。该数组只包含我直接由objectAtIndexPath设置的字符串对象,但我决定将它直接设置为@hi,因为它不起作用。我甚至将setCellLabel方法中使用的值更改为硬编码值,并且每个单元格中只保留Label默认字符串。



为什么cellLabel没有正确设置?

解决方案

您是否在界面构建器中设置了cellLabel插座?



<你应该在你的单元格的.h文件中有这样的东西:

  @property(弱,非原子)IBOutlet UILabel * cellLabel ; 

然后您真的需要这样做:

  cell.cellLabel.text = @; 


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?

解决方案

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

这篇关于如何在UICollectionViewCell中设置UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆