无法让uicollectionviewheader工作



相关代码:

控制器:

- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(106.0, 106.0);
    layout.minimumInteritemSpacing = 1.0;
    layout.minimumLineSpacing = 1.0;
    layout.headerReferenceSize = CGSizeMake(320.0, 44.0);
    return (self = [super initWithCollectionViewLayout:layout]);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // some setup
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ITPhotosHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView;
    if (kind == UICollectionElementKindSectionHeader) {
        ITPhotosHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                          withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerView = [[ITPhotosHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
        reusableView = headerView;
    }
    return reusableView;
}

下面是我得到的错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (<ITPhotosHeaderView: 0x1781b9de0; baseClass = UICollectionReusableView; frame = (0 0; 320 44); layer = <CALayer: 0x17802f980>>)

我调试并确保它没有返回nil。因此,我觉得是registerClass部分工作不正常。我将感谢任何意见。谢谢。

这里有一个明确的问题:

    ITPhotosHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                      withReuseIdentifier:@"header" forIndexPath:indexPath];
    headerView = [[ITPhotosHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];

那个代码是没有意义的。在第一行中,您取消了头部视图的队列。在第二行中,您随意丢弃刚刚退出队列的header视图,并创建一个全新的header视图。

相关内容

  • 没有找到相关文章

最新更新