自定义UICollectionViewCell中的UILabel始终为null,无法更新文本

bro*_*ney 16 storyboard uilabel ios ios6 uicollectionviewcell

我为Apple集合视图示例项目创建了一个非常简单的集合视图.我在storyboard中的视图控制器中有一个集合视图,并在集合视图右上角的集合视图单元格内设置一个标签.我已经把它连接到我的自定义单元格中的IBOutlet.这是相关的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
    ...
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collView) {
        Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];    
        cell.segmentTitle.text = @"some text";
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

我在segmentTitle.textpart 之后放了一个断点,segmentTitle总是为null.因此,我在模拟器中看到的是空白框.我错过了什么?

LE *_*ANG 41

StoryBoard里面的UICollectionViewCell不需要registerClass,只需在StoryBoard中选择reuseidentifier即可.删除此行:

   // [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
Run Code Online (Sandbox Code Playgroud)

并确保您正确连接:

- 在storyBoard中选择类的UICollectionViewCell到Cell

-Drag UILabel进入Cell并连接到Cell.h

- 键入重用标识符

  • 删除注册行并使用故事板中的重用标识符修复它.谢谢! (5认同)
  • 这有效.有时在故事板中有太多魔力. (2认同)