4

我有一个UIView子类,它有一个动态创建的UICollectionView。一切工作正常在ios7中显示标题就好了。UICollectionView部分标题崩溃iOS 8

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; 
} 
+0

什么是崩溃消息? – shawnwall 2014-10-16 15:02:37

+0

不幸的是我实际上并没有获得堆栈跟踪。我尝试了符号断点和默认异常处理程序。我正在使用库进行分析,它们显示的崩溃日志是: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'*** - [__ NSArrayM insertObject:atIndex:]:object can not be nil' 我不是在那里的任何地方调用insertObject,所以我不太确定这是否是一个准确的崩溃日志 – 2014-10-16 15:07:43

+2

我敢肯定这是一个错误。 http://stackoverflow.com/questions/25460323/why-does-using-headerreferencesize-with-self-sizing-cells-in-a-collection-view-c – 2014-10-27 10:46:06

回答

1

我刚刚回看这又是我能得到的页眉和页脚一段时间在不同的视图中工作,发现这个特定视图的问题是设置estimateSize参数。

注释掉这一行摆脱了我的烦恼:

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

这是一个令人沮丧的诊断,因为没有错误,编译器警告等给予的任何信息。

+0

请打开一个错误报告。 – DrMickeyLauer 2015-06-01 14:54:39