【问题标题】:UILabel not showing in UICollectionViewUICollectionView 中未显示 UILabel
【发布时间】:2012-12-03 13:43:04
【问题描述】:

我有一个带有 UICollectionViewCell 对象的 UILabel。

@interface TideDataTableCell : UICollectionViewCell

@property (strong, nonatomic) NSString* dayString;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;

@end

标签在单元对象的 m 文件中合成。但是,当我尝试分配 text 属性时,标签对象始终为空。即使创建一个新标签并将其分配给单元格 dayLabel 也不起作用!下面的代码只是对标签的直接分配,因为似乎没有任何工作......

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Main Tide Data Table Cell";

    TideDataTableCell* tideDayDataCell = [self.tideDataTable dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    tidalDate* tideDate = self.tidalDates[indexPath.row];
    self.tideDataTable.backgroundColor = [UIColor lightGrayColor];
    tideDayDataCell.backgroundColor = [UIColor whiteColor];
    tideDayDataCell.dayLabel.textColor = [UIColor blackColor];
    tideDayDataCell.dayLabel.text = tideDate.dateString;
    return tideDayDataCell;
}

为什么这不起作用?!我已经检查了 UICollectionViewCell 中的标签是否连接到单元格 h 文件中的 dayLabel(上图)

【问题讨论】:

  • 您是否尝试过让 dayLabel 变强或保留?可能是在重用单元之前释放它,因为它是弱属性。
  • 您好...我刚刚尝试使它变得强大并保留 - 但它仍然不起作用...
  • 好的...我已经取得了一些进展...我之前一定做错了 - 我创建了一个新标签并将其分配给 dayLabel 属性。标签 - 虽然它不再是 null 仍然不会显示在 uicollectionview
  • 所有排序 - 我已按照教程进行操作,但忘记在 viewDidLoad 中注释掉一行... Duh!

标签: ios uicollectionview uicollectionviewcell


【解决方案1】:

您需要像这样在 TideDataTableCell 的 viewDidLoad 上注册您的单元格:

UINib *cellNibName = [UINib nibWithNibName:@"cellNibName" bundle:nil];
[self.collectionView registerNib:cellNibName forCellWithReuseIdentifier:@"cellIdentifier"];

然后,在 cellForItemAtIndexPath 中,您必须获取单元格并使用它:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"cellIdentifier";

    TideDataTableCell* tideDayDataCell = [self.tideDataTable dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    tidalDate* tideDate = self.tidalDates[indexPath.row];
    self.tideDataTable.backgroundColor = [UIColor lightGrayColor];
    tideDayDataCell.backgroundColor = [UIColor whiteColor];
    tideDayDataCell.dayLabel.textColor = [UIColor blackColor];
    tideDayDataCell.dayLabel.text = tideDate.dateString;
    return tideDayDataCell;
}

不要忘记在 xib 文件中设置重用标识符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多