iOS:UICollectionView实现ViewForSupplementElementofKind

iOS:UICollectionView实现ViewForSupplementElementofKind,ios,objective-c,xcode,Ios,Objective C,Xcode,我使用此方法为UICollectionView节提供一个简单的哑标题: - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *header = nil;

我使用此方法为UICollectionView节提供一个简单的哑标题:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *header = nil;

    NSString *sectionTitle;
    switch (indexPath.section) {
        case 0:
            sectionTitle = @"One";
            break;
        case 1:
            sectionTitle = @"Two";
            break;
        case 2:
            sectionTitle = @"Thre";
            break;
        case 3:
            sectionTitle = @"Four";
            break;
    }

    if (kind == UICollectionElementKindSectionHeader) {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"headerView"
                                                           forIndexPath:indexPath];

        UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        sectionLabel.text = sectionTitle;
        [self.rosterCollectionView addSubview:sectionLabel];
    }


    return header;
}
它在
dequeueReusableSupplementaryViewOfKind
调用中爆炸:

***在-[UICollectionView\u dequeueReusableViewOfKind:withIdentifier:forIndexPath:],/SourceCache/UIKit/UIKit-2903.23/UICollectionView.m:3117中断言失败


这里出了什么问题?

在使用出列方法之前,应该使用
registernb:forSupplmentaryViewofKind:withReuseIdentifier:
registerClass:forSupplmentaryViewofKind:withReuseIdentifier:
注册类/nib,如果它只是我在代码中创建的
UIView
呢?这是一个愚蠢的简单视图,带有
UILabel
。只需将UIView和register子类化。文档链接::