objective-c – UICollectionView部分标题崩溃iOS 8

我有一个UIView子类,它有一个动态创建的UICollectionView.在ios7中一切正常,显示标题就好了.

iOS 8根本不调用此方法,应用程序崩溃.

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

如果我注释掉这个方法并且我将页眉/页脚大小设置为0,则iOS 8不会再崩溃.

以下是创建集合视图的代码:

int spacing = 39;

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(199, 60);
if (IS_IOS8) layout.estimatedItemSize = CGSizeMake(199, 60);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumInteritemSpacing = 1;
layout.minimumLineSpacing = 1;
layout.sectionInset = UIEdgeInsetsMake(1,1,1,1);
layout.headerReferenceSize = CGSizeMake(self.width - spacing*2, 50.0f);
layout.footerReferenceSize = CGSizeZero;

self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(spacing, 5, self.width - spacing*2, self.height - spacing*2) collectionViewLayout:layout];

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

[self.collectionView registerClass:[OCEListCell class] forCellWithReuseIdentifier:CellIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderCellIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterCellIdentifier];
self.collectionView.backgroundColor = [UIColor clearColor];


[self addSubview:self.collectionView];

数据源方法:

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

    NSLog(@"viewForSupplementaryElementOfKind");
    if ([kind isEqualToString:UICollectionElementKindSectionHeader] )
    {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderCellIdentifier forIndexPath:indexPath];
        view.backgroundColor = [UIColor clearColor];

        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, view.width, 40)];
        lbl.textColor = [UIColor defaultTextColor];
        lbl.font = [UIFont bookFontOfSize:25.0f];
        lbl.numberOfLines = 1;
        lbl.text = indexPath.section == 0 ? @"Section 1 Header" : @"Section 2 Header";
        [view addSubview:lbl];
    }
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
    {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:FooterCellIdentifier forIndexPath:indexPath];
        view.backgroundColor = [UIColor clearColor];
    }

    NSLog(@"viewForSupplementaryElementOfKind:%@", view);
    return view;
}



-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = CGSizeMake(collectionView.width, 50);
    NSLog(@"referenceSizeForHeaderInSection: %@", NSStringFromCGSize(size));
    return size;
}


-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    CGSize size = CGSizeZero;
    NSLog(@"referenceSizeForFooterInSection: %@", NSStringFromCGSize(size));
    return size;
}

最佳答案 我回过头来再看一遍,因为我能够让页眉和页脚在不同的视图中工作一段时间,并发现这个特定视图的问题是设置estimateSize参数.

评论这条线摆脱了我的烦恼:

if (IS_IOS8) layout.estimatedItemSize = CGSizeMake(177, 60);

诊断这是一个令人沮丧的事情,因为没有错误,编译器警告等提供任何信息.

点赞