【问题标题】:UICollectionViewCell with Interface Builder带有界面生成器的 UICollectionViewCell
【发布时间】:2014-12-15 08:41:51
【问题描述】:

我在 Interface Builder 中有以下 nib 设置,其中包含我的 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"];

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

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

生成的dayLabel 为零,因此我无法为其分配值,并且我的视图什么也不显示。这同样适用于我的可重用标题视图及其年份和月份标签。

我在这里做错了什么?

【问题讨论】:

    标签: ios objective-c uicollectionviewcell


    【解决方案1】:

    我在这里做错了什么?

    几件事:

    • 为单元格重用注册一个 nib 要求单元格是该 nib 中的顶级对象 - 您不能拥有这样的多内容 nib。要么将单元拉出到单独的笔尖,要么使用故事板和原型
    • 注册一个 用于单元重用意味着您将在代码中构建单元的内容,我猜您并没有这样做。注册类不知道该 nib 文件。
    • 标记几乎总是错误的做法 - 创建一个子类并使用插座代替。

    【讨论】:

    • 感谢您的建议 - 我为单元格和标题视图创建了单独的类,现在它可以工作了
    猜你喜欢
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多