简体   繁体   English

水平设置UICollectionView的标头时,应用程序崩溃

[英]Application crash when set header of UICollectionView horizontally

I am pretty new in UICollectionView. 我在UICollectionView中很新。 And I am really tired to find out the solution. 我真的很累,无法找到解决方案。 I am trying to add Header in 3 Horizontally Row. 我想在3水平行中添加标题。 I am using Collection view flow layout. 我正在使用“收藏夹”视图流布局。
Here is my code which I implement: 这是我实现的代码:

    - (void)awakeFromNib {

    self.collectionView.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];

    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(130.0, 170.0);
    [self.collectionView setCollectionViewLayout:flowLayout];

    // Register the colleciton cell
    [_collectionView registerNib:[UINib nibWithNibName:@"ORGArticleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ORGArticleCollectionViewCell"];
    [self.collectionView registerClass:[_HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}
#pragma mark - UICollectionViewDataSource methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return [self.collectionData count];
    }
    else if(section == 1)
    {
        return [self.collectionData1 count];
    }
    else
    {
        return [self.collectionData2 count];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ORGArticleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ORGArticleCollectionViewCell" forIndexPath:indexPath];

    cell.articleTitle.text = [self.collectionData objectAtIndex:[indexPath row]];
    NSString *URL = [self.collectionImageData objectAtIndex:indexPath.row];
    [cell.articleImage setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]];
    cell.articleImage.contentMode = UIViewContentModeScaleToFill;
    cell.articlePrice.text = [self.collectionDataPric objectAtIndex:[indexPath row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didSelectItemFromCollectionView" object:cellData];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[headerView viewWithTag:10];
    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
        label.tag = 10;
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor darkGrayColor];
        [headerView addSubview:label];
    }

    label.text = [NSString stringWithFormat:@"Section %d", indexPath.section];
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize headerSize = CGSizeMake(320, 44);
    return headerSize;
}

My App crash in viewForSupplementaryElementOfKind method when I initialize the header view. 初始化标题视图时,我的App在viewForSupplementaryElementOfKind方法中崩溃。
Following are the crash log: 以下是崩溃日志:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionHeader at path {length = 2, path = 0 - 0}' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“-layoutAttributesForSupplementaryElementOfKind没有UICollectionViewLayoutAttributes实例:UICollectionElementKindSectionHeader位于路径{length = 2,路径= 0-0}”

The problem with your code is [headerView addSubview:label]; 您的代码存在的问题是[headerView addSubview:label]; this is called only when label is equal to nil. 仅当label等于nil时才调用此方法。 but you have to call this statement every time when viewForSupplementaryElementOfKind is called. 但您每次调用viewForSupplementaryElementOfKind时都必须调用此语句。

Update your viewForSupplementaryElementOfKind function with following code. 使用以下代码更新viewForSupplementaryElementOfKind函数。

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

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[headerView viewWithTag:10];
    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
        label.tag = 10;
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor darkGrayColor];

    }
    label.text = [NSString stringWithFormat:@"Section %d", indexPath.section];
    [headerView addSubview:label];
    return headerView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM