UILabel没有在UICollectionView中显示

时间:2012-12-03 13:43:05

标签: ios uicollectionview uicollectionviewcell

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

@interface TideDataTableCell : UICollectionViewCell

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

@end

标签在m文件中合成为单元对象。当我尝试分配text属性时,label对象始终为null。即使创建一个新标签并将其分配给单元格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(上图)

1 个答案:

答案 0 :(得分:0)

您需要在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文件中设置重用标识符。