用于节标题的UICollectionReusableView无法正常工作

Sub*_*ash 5 objective-c ios uicollectionview uicollectionreusableview

我为UICollecton视图节标题创建了一个UICollectionReusuable视图.我使用以下代码实现头视图.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
ThemeHeader *headerView = [[ThemeHeader alloc] init];
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                 withReuseIdentifier:@"header"
                                                                        forIndexPath:indexPath];
NSString *title = @"Title for the header";
headerView.title.text = title;
return headerView;
Run Code Online (Sandbox Code Playgroud)

}

它崩溃了,给我以下错误:

- [UICollectionReusableView title]:无法识别的选择器发送到实例0xac846a0'

我的ThemeHeader类看起来像这样

@interface ThemeHeader : UICollectionReusableView
@property (strong, nonatomic) IBOutlet UILabel *title;

@end
Run Code Online (Sandbox Code Playgroud)

我提前感谢您的帮助.

Yas*_* T. 7

它意味着headerView不是ThemeHeader您期望的实例,但其实例UICollectionReusableView没有title属性.

可能是因为您可能没有ThemeHeader在故事板上的身份检查器中为此可恢复视图设置为自定义类.

  • 不知道为什么这会有一个downvote.对我来说似乎是一个很好的答案. (2认同)