gpt4 book ai didi

ios - UICollectionViewCell 与界面生成器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:53 25 4
gpt4 key购买 nike

我在 Interface Builder 中有以下 nib 设置,其中包含我的 viewController 的主视图(带有 UICollectionView)以及标题和单元格的可重用 View

enter image description here

我在 viewDidLoad 中注册可重用 View

[self.calendarView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dayCell"];
[self.calendarView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dayCellDisabled"];
[self.calendarView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"monthHeader"];

当我尝试像这样访问我的单元格标签时:

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dayCellDisabled" forIndexPath:indexPath];    
UILabel *dayLabel = (UILabel *)[cell viewWithTag:1];

生成的 dayLabel 为 nil,因此我无法为其赋值,并且我的 View 不显示任何内容。这同样适用于我的可重用标题 View 及其年份和月份标签。

我在这里做错了什么?

最佳答案

What am I doing wrong here?

几件事:

  • 为单元格重用注册一个 nib 要求单元格是该 nib 中的顶级对象 - 你不能拥有像这样的多内容 nib。要么将单元拉出到单独的 Nib 中,要么使用 Storyboard和原型(prototype)
  • 为单元格重用注册一个意味着您将在代码中构建单元格的内容,鉴于上述情况,我猜您并没有这样做。已注册的类不知道该 nib 文件。
  • 标签几乎总是错误的做法——创建一个子类并改用 outlets。

关于ios - UICollectionViewCell 与界面生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27480153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com