相关疑难解决方法(0)

如何在UICollectionViewCell中设置UILabel

我有一个带有UIViewController的应用程序,里面有一个UICollectionView IBOutlet.

MyCustomCell Outlet已连接 MyCustomCell ReuseID MyCustomCell类标识

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

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

单元格具有属性类型类型,在故事板中将其标识为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]);
    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;
}
Run Code Online (Sandbox Code Playgroud)

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

为什么cellLabel没有正确设置?

ios uicollectionviewcell

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

ios ×1

uicollectionviewcell ×1