【问题标题】:UILabel in custom UICollectionViewCell always null, can't update text自定义 UICollectionViewCell 中的 UILabel 始终为空,无法更新文本
【发布时间】:2023-03-14 13:19:01
【问题描述】:

我创建了一个对 Apple 集合视图示例项目非常简单的集合视图。我在情节提要的视图控制器中有一个集合视图,并在集合视图右上角的集合视图单元格内设置了一个标签。我已将其连接到自定义单元格中的 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 始终为空。因此,我在模拟器中看到的是空的白框。我错过了什么?

【问题讨论】:

    标签: ios ios6 storyboard uilabel uicollectionviewcell


    【解决方案1】:

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

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

    并确保以正确的方式连接:

    -将storyBoard中UICollectionViewCell的类类型选择为Cell

    -将 UILabel 拖入 Cell 并挂接到 Cell.h

    -键入重用标识符

    【讨论】:

    • 删除注册行并使用情节提要中的重用标识符修复了它。谢谢!
    • 这行得通。有时情节提要中有太多的魔力。
    • 抱歉 -Type 重用标识符是什么意思?我删除了第一行,但什么都看不到(做了其他步骤)
    • @Signo 在你的故事板中输入它
    • 是的,找到了 :) 现在我只需要了解为什么宽度和高度没有更新,我很好:P 谢谢!
    【解决方案2】:
    MainFeedCollectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2019-09-19
      • 2021-09-08
      相关资源
      最近更新 更多