如何在UICollectionViewCell中设置UILabel

3

我有一个包含一个 UICollectionView IBOutlet 的 UIViewController 应用程序。

MyCustomCell Outlet connected MyCustomCell ReuseID MyCustomCell Class Identity

UICollectionView 中的单元格是 MyCustomCell,它有一个设置其 UILabel 的方法:

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

在Storyboard中,该单元格具有Class类型属性,标识为MyCustomCell,并设置它的dequeue identifier。UIViewController采用了数据源和委托协议。IBOutlet UILabel的cellLabel插座已连接到Storyboard。方法定义如下:

- (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;
}

我在我的tableview中画了三个单元格,对应于我的数组中的3个对象。该数组只包含字符串对象,我最初通过objectAtIndexPath直接设置它们,但是由于它不起作用,我决定直接将其设置为@"hi"。我甚至将setCellLabel方法中使用的值更改为硬编码值,但每个单元格仍然只得到了默认的“Label”字符串。

为什么cellLabel没有被正确设置?

3个回答

5
当您使用Storyboard时,默认模板是错误的,因为它使用了

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

但是故事板已经将其注册了。所以,基本上这就是创建一个基于故事板的自定义UICollectionView需要做的:

  1. 创建你的UICollectionViewController子类
  2. 点击并拖拽一个UICollectionViewController到故事板中,并按照你想要的配置更改它
  3. 创建一个UICollectionViewCell子类
  4. 在故事板中设置单元格类,设置它的重用标识符,控制-拖动所需的输出
  5. 删除 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: CellIdentifier];
  6. UICollectionView子类中设置静态重用标识符

5

您是否在界面构建器中设置了cellLabel输出口?

你应该在cell的.h文件中有类似这样的代码:

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

稍后您只需要这样做:
cell.cellLabel.text = @"";

是的...它与UICollectionViewCell标签相关联。 - marciokoko
13
你是否再次调用 [self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier: CellIdentifier]?如果你使用Storyboard,就不应该这么做。它会覆盖你在Storyboard中设置的内容。 - Patrick Tescher
我删除了这行代码,因为我正在使用Storyboard,但是我只得到默认的“Label”文本。如果加上这行代码,我会得到一个空白单元格,这应该是可以预料的。问题在于标签没有从默认的LABEL设置为我的值。 - marciokoko
1
你需要深入挖掘一下。当你调用setCellLabel时,cellLabel是否为nil?另外,你应该使用self.cellLabel。 - Patrick Tescher
Patrick,谢谢,我将IBOutlet UILabel作为ivar,所以我将其设置为属性。现在我使用self.cellLabel.text = @"string"而不是只有cellLabel.text = ...。但是,在cellForItemAtIndexPath中,我添加了一个if测试来检查cell.cellLabel == nil,它确实记录为nil。 - marciokoko
显示剩余6条评论

0
你可能忘了告诉你的 `UICollectionView` 该使用哪个类来创建单元格。可以通过以下代码来完成这个任务:
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier: CellIdentifier];

你可以在控制器的viewDidLoad方法末尾设置这个。


1
如果您使用接口生成器,那么就不需要运行此命令。 - Patrick Tescher
我正在使用Storyboard...在我的viewDidLoad方法中有这一行。 - marciokoko
请确保您的插座设置正确(针对UICollectionView和UICollectionViewCell)。 - tiguero
在 [cell setCellLabel:@"hi"]; 之后添加 [cell setNeedsDisplay]; ,这样做可以强制刷新单元格,你觉得呢? - tiguero
我删掉了我的[registerClass]代码行,现在它可以工作了。 - Ben Clayton
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接