繁体   English   中英

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

[英]UILabel in custom UICollectionViewCell always null, can't update text

我为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;
}

我在segmentTitle.text部分之后放置了一个断点,而segmentTitle始终为null。 因此,我在模拟器中看到的是空白框。 我错过了什么?

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

   // [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];

并确保您正确连接:

- 在storyBoard中选择类的UICollectionViewCell到Cell

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

- 键入重用标识符

MainFeedCollectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")

我从我的代码中删除了这一行,现在它工作正常......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM