viewForSupplementaryElementOfKind由于空数组而无法正常工作

时间:2014-04-06 08:12:16

标签: ios uicollectionview uicollectionreusableview

有时候(虽然数据没有变化,但每次都不会发生),我的uiCollectionView中的页眉/页脚无法实例化,应用程序崩溃,因为我收到以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

当我的页脚位于奇数段时,我将其页脚大小设置为0.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view;
    NSLog(@"indexpath section %ld", (long)indexPath.section);
    if(kind == UICollectionElementKindSectionHeader){

        HeaderViewQuestion *headerView = [collectView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewQuestion" forIndexPath:indexPath];
        NSString *initial = [[NSString alloc] initWithFormat:@"Question %d : ",(int)indexPath.section+1];
        NSMutableString *text = [[NSMutableString alloc] initWithString:initial];
        [text appendString:[questions objectAtIndex:indexPath.section]];
        NSInteger _stringLength=[text length];

        NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:text];

        [attString addAttribute:NSFontAttributeName
                          value:[UIFont fontWithName:@"Helvetica" size:20.0]
                          range:NSMakeRange(0, _stringLength)];
        [attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, _stringLength)];

        headerView.label.attributedText = attString;
        view = headerView;
    }
    if(kind == UICollectionElementKindSectionFooter){
        FooterView *footerview = (FooterView*)[collectView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
            footerview.addLine.tag = indexPath.section;
            [footerview.addLine addTarget:self action:@selector(addLine:) forControlEvents: UIControlEventTouchUpInside];
            view = footerview;
    }
    return view;
}

- (CGSize)collectionView:(UICollectionView *)collecView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if(section%2 !=0)
    {
        return CGSizeZero;
    }
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
    return CGSizeMake(flowLayout.itemSize.width, 100);
}
- (CGSize)collectionView:(UICollectionView *)collecView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
    return CGSizeMake(flowLayout.itemSize.width, 100);
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

删除以下行后:

UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
            [flowLayout setItemSize:CGSizeMake(screenWidth-6, 50)];
        }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
            [flowLayout setItemSize:CGSizeMake(screenHeight-24, 50)];
        }
方法中的

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectView cellForItemAtIndexPath:(NSIndexPath *)indexPath

问题似乎消失了。我认为在我的UiCollectionView中设置单元格花费了很多时间,这导致了我的页脚/标题和空数组的问题。

这是我的代码:

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *view;
    if(indexPath.section %2 == 0){
        //create collection view cell
        QuestionCell *cell = (QuestionCell *)[collectView dequeueReusableCellWithReuseIdentifier:@"QuestionCell" forIndexPath:indexPath];
        [cell setTag:indexPath.section];
        //configure cell :
        [cell.textView setTag:indexPath.item];
        [cell.suppress setTag:indexPath.section];
        [cell.suppress addTarget:self action:@selector(deleteLine:) forControlEvents: UIControlEventTouchUpInside];
        view = cell;
    }else{
        YesNo *cell = (YesNo*)[collectView dequeueReusableCellWithReuseIdentifier:@"YesNo" forIndexPath:indexPath];
        UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
            UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
            if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
                [flowLayout setItemSize:CGSizeMake(screenWidth-6, 50)];
            }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
                [flowLayout setItemSize:CGSizeMake(screenHeight-24, 50)];
            }
        [cell setTag:indexPath.section];

        view = cell;
    }
    return view;
}

我改为:

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *view;
    if(indexPath.section %2 == 0){
        //create collection view cell
        QuestionCell *cell = (QuestionCell *)[collectView dequeueReusableCellWithReuseIdentifier:@"QuestionCell" forIndexPath:indexPath];
        [cell setTag:indexPath.section];
        //configure cell :
        [cell.textView setTag:indexPath.item];
        [cell.suppress setTag:indexPath.section];
        [cell.suppress addTarget:self action:@selector(deleteLine:) forControlEvents: UIControlEventTouchUpInside];
        view = cell;
    }else{
        YesNo *cell = (YesNo*)[collectView dequeueReusableCellWithReuseIdentifier:@"YesNo" forIndexPath:indexPath];
        [cell setTag:indexPath.section];

        view = cell;
    }
    return view;
}