UICollectionView: пользовательский сбой FlowLayout при пустых разделах

У меня проблема с использованием пользовательского макета потока, если мои разделы пусты, я получаю "EXC_ARTHMETIC(code=EXC_I386_DIV, subcode=0x0)".

Я использую пользовательский FlowLayout для добавления пользовательского заголовка и фона пользовательского раздела (сбой связан с фоном).
некоторый код (пожалуйста, посмотрите на layoutAttributesForBackgroundAtSection:, чтобы увидеть, где происходит сбой):

//  UIViewController 

   -(void)loadView
   {
    [super loadView];

    collectionViewFlowLayout *flowLayout = [[collectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(35, 20, 0, 20);
    [flowLayout setItemSize:cellSize];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.minimumInteritemSpacing = 0.0f;

    self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRectZero) collectionViewLayout:flowLayout];
    [self.collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.collectionView setBackgroundColor:[UIColor colorWithRed:246./256. green:246./256. blue:246./256. alpha:1]];
    [self.view addSubview:self.collectionView];

    [[self.collectionView.topAnchor constraintEqualToAnchor:self.view.topAnchor] setActive:YES];
    [[self.collectionView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor] setActive:YES];
    [[self.collectionView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor] setActive:YES];
    [[self.collectionView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor] setActive:YES];

    [self.collectionView registerClass:[collectionViewCell class] forCellWithReuseIdentifier:reuseCellIdentifier];
    [self.collectionView registerClass:[sectionHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseHeaderIdentifier];
    [self.collectionView registerClass:[sectionBackgroundReusableView class]
            forSupplementaryViewOfKind:KindSectionBackground
                   withReuseIdentifier:NSStringFromClass([sectionBackgroundReusableView class])];

    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    [self.collectionView setShowsVerticalScrollIndicator:YES];

    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];
   }



   -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
      return 6;
    }

   -(NSInteger)collectionView:(UICollectionView *)collectionView     numberOfItemsInSection:(NSInteger)section
    {
      return 0;
    }


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

    if (kind == UICollectionElementKindSectionHeader)
    {
        reusableview = [collectionV dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseHeaderIdentifier forIndexPath:indexPath];
        NSString *sectionName = self.shelves[indexPath.section];

        [(sectionHeaderReusableView*)reusableview configureWithTitle:sectionName];
    }
    if (kind == UICollectionElementKindSectionFooter)
    {

    }
    if (kind == KindSectionBackground)
    {
        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:sectionBackground
                                                          withReuseIdentifier:NSStringFromClass([sectionBackgroundReusableView class])
                                                                 forIndexPath:indexPath];
    }
    return reusableview;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return = KHeaderSize;
}

// Подкласс FlowLayout

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *attributes = [NSMutableArray arrayWithArray:[super layoutAttributesForElementsInRect:rect]];

    // 1. get visible sections
    NSInteger lastIndex = -1;
    for(UICollectionViewLayoutAttributes * attr in attributes) {
        lastIndex = attr.indexPath.section;
        UICollectionViewLayoutAttributes * attr = [self layoutAttributesForBackgroundAtSection:lastIndex];
        [attributes addObject:attr];
    }

    return attributes;
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind
                                                                     atIndexPath:(NSIndexPath *)indexPath
{
    if([kind isEqualToString:KindSectionBackground]) {
        return [self layoutAttributesForBackgroundAtSection:indexPath.section];
    } else {
        return [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
    }
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForBackgroundAtSection:(NSUInteger)section
{
    NSIndexPath * indexPath =[NSIndexPath indexPathForItem:0
                                                 inSection:section];
    UICollectionViewLayoutAttributes * attr = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:KindSectionBackground
                                                                                                             withIndexPath:indexPath];
    attr.hidden = NO;
    attr.zIndex = -1;


  // crash happen in the line below.
  UICollectionViewLayoutAttributes * firstAttr =   [selflayoutAttributesForItemAtIndexPath:indexPath];


    CGRect frame;
    frame.origin.x = firstAttr.frame.origin.x - self.sectionInset.left;
    frame.origin.y = firstAttr.frame.origin.y - self.sectionInset.top;

    frame.size.width = self.collectionView.bounds.size.width;

    NSUInteger numItems = [self.collectionView numberOfItemsInSection:section];

    CGFloat cellsPerLine = floorf(self.collectionView.bounds.size.width / self.itemSize.width);
    NSUInteger numLines = ceilf(numItems / (float)cellsPerLine);

    frame.size.height = numLines * firstAttr.size.height + (numLines-1)*self.minimumLineSpacing +
    self.sectionInset.top + self.sectionInset.bottom;

    attr.frame = frame;

    return attr;
}

Не могли бы вы помочь исправить это, спасибо.


person Red Mak    schedule 17.08.2016    source источник


Ответы (2)


Похоже, что layoutAttributesForItemAtIndexPath дает сбой, когда раздел, указанный в IndexPath, пуст. Вероятно, это ошибка, поскольку она должна возвращать необязательное значение (по крайней мере, в Swift). У меня была такая же проблема, и я исправил ее, проверив количество элементов в разделе (быстрый пример для раздела 1):

if dataSource.collectionView(collectionView, numberOfItemsInSection: 1) > 0 {    
    if let layoutAttributes = self.layoutAttributesForItem(at: IndexPath(row: 0, section: 1)) {
        // update layoutAttributes or create your background attributes
    }
}
person Alexis    schedule 29.06.2017

Одним из обходных путей было бы вызвать collectionView.reloadData() заранее, прежде чем мы вызовем какие-либо методы макета, это работает для меня.

person boog    schedule 02.11.2018