简体   繁体   English

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

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

I created a collection view very simple to an Apple collection view sample project. 我为Apple集合视图示例项目创建了一个非常简单的集合视图。 I have a collection view in a view controller in storyboard, and set a label inside the collection view cell in the top right part of the collection view. 我在storyboard中的视图控制器中有一个集合视图,并在集合视图右上角的集合视图单元格内设置一个标签。 I've hooked that up to the IBOutlet in my custom cell. 我已经把它连接到我的自定义单元格中的IBOutlet。 Here's the relevant code: 这是相关的代码:

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

I put a breakpoint after the segmentTitle.text part and segmentTitle is always null. 我在segmentTitle.text部分之后放置了一个断点,而segmentTitle始终为null。 Accordingly what I see in the simulator is empty white boxes. 因此,我在模拟器中看到的是空白框。 What did I miss? 我错过了什么?

UICollectionViewCell inside StoryBoard don't need to registerClass, just choose reuseidentifier in StoryBoard. StoryBoard里面的UICollectionViewCell不需要registerClass,只需在StoryBoard中选择reuseidentifier即可。 Delete this line: 删除此行:

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

And make sure you connect right way: 并确保您正确连接:

-Select class type of UICollectionViewCell in storyBoard to Cell - 在storyBoard中选择类的UICollectionViewCell到Cell

-Drag UILabel into Cell and hook up to Cell.h -Drag UILabel进入Cell并连接到Cell.h

-Type the reuse identifier - 键入重用标识符

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

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

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

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