1

我有一个 UICollectionViewController 代表 UICollectionViewDataSource 和 UICollectionViewDelegate。我的集合视图显示 2 个部分,其中包含多行数据并且工作正常。

我创建了一个部分标题(在 IB 属性检查器 -> 附件中),然后使用 SWOHighScoreHeader 类对 UICollectionReusableView 进行子类化:

@interface SWOHighScoreHeader : UICollectionReusableView
@property (strong, nonatomic) IBOutlet UILabel *hiScoreHead;
@end

我将此类 (SWOHighScoreHeader) 设置为 IB 中 UICollectionReusableView 的自定义类。

在 UICollectionViewController 我使用的方法:

-(UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

SWOHighScoreHeader *highScoreHeaderView = nil;

if ([kind isEqual:UICollectionElementKindSectionHeader]) {
    highScoreHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                             withReuseIdentifier:@"highScoreTableHead"
                                                                    forIndexPath:indexPath];
}

return highScoreHeaderView;
}

标识符highScoreTableHead设置为 IB 中的 UICollectionReusableView Collection Reusable View Identifier。

在这个阶段,节标题正确显示,尽管带有默认标签文本。

当我将 IBOutlet UILabel hiScoreHead属性与 IB 中的插座连接时,我的问题就出现了。当我这样做时,程序崩溃:

Interface Builder 文件中的未知类 SWOHighScoreHeader。

** * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 submitButton 的键值编码不兼容。”

我试过删除插座连接并重新连接,但仍然没有。有什么想法我哪里出错了吗?

4

3 回答 3

0

将以下属性添加到 SWOHighScoreHeader:

@property (weak) IBOutlet UIButton* submitButton;

或者尝试找出情节提要中的哪个对象期望 submitButton 实际存在。

于 2015-04-02T18:57:44.407 回答
0

IBoutlet 必须始终是弱属性,否则不会释放其容器。

于 2015-04-02T19:04:15.697 回答
0

我通过使用标签而不是 IBOutlets 解决了这个问题,即

UILabel *hiScoreHeader = (UILabel *)[highScoreHeaderView viewWithTag:101];
hiScoreHeader.text = @"Header Text";

我不确定为什么 IBOutlets 不起作用,但至少我有一个解决方案。

于 2015-04-02T20:29:01.443 回答