带有接口生成器的UICollectionViewCell

art*_*ras 2 objective-c ios uicollectionviewcell

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

在此处输入图片说明

我在viewDidLoad中注册可重用的视图

[self.calendarView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dayCell"];
[self.calendarView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dayCellDisabled"];
[self.calendarView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"monthHeader"];
Run Code Online (Sandbox Code Playgroud)

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

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

结果dayLabel为nil,因此无法为其分配值,并且视图不显示任何内容。我的可重复使用标题视图及其“年”和“月”标签也是如此。

我在这里做错了什么?

jrt*_*ton 5

我在这里做错了什么?

几件事:

  • 注册可用于单元复用的笔尖要求该单元格是该笔尖中的顶级对象-您不能拥有像这样的多内容笔尖。将单元拉出到单独的笔尖中,或者使用情节提要和原型
  • 注册一个用于单元重用的意味着您将在代码中构建单元的内容,鉴于以上所述,我想您没有这样做。注册的班级不知道该nib文件。
  • 标记几乎总是错误的做法-创建子类并改用插座。