viewForSupplementaryElementOfKind 由于空数组而不起作用



有时(即使数据没有更改,也不会每次都发生),我的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);
}

我找到了解决方案:

删除以下行后:

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

相关内容

  • 没有找到相关文章

最新更新