【问题标题】:Application crash when set header of UICollectionView horizontally水平设置 UICollectionView 的标头时应用程序崩溃
【发布时间】:2015-09-27 09:09:50
【问题描述】:

我是 UICollectionView 的新手。我真的很累找到解决方案。我正在尝试在 3 Horizo​​ntally Row 中添加标题。我正在使用集合视图流布局。
这是我实现的代码:

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

当我初始化标题视图时,我的应用程序在 viewForSupplementaryElementOfKind 方法中崩溃。
以下是崩溃日志:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用,原因:“-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例:路径 {length = 2, path = 0 - 0} 处的 UICollectionElementKindSectionHeader”

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    您的代码的问题是[headerView addSubview:label];,仅当 label 等于 nil 时才会调用它。但是每次调用viewForSupplementaryElementOfKind时都必须调用这个语句。

    使用以下代码更新您的 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;
    }
    

    【讨论】:

    • 在初始化collection view之前把所有的flow layout代码放好。
    • 你没有初始化collectionView
    • 为什么要为 collectionView 背景视图初始化另一个视图?我不明白你的意思
    • 有没有你在使用[UICollectionView alloc] init这个代码的地方
    • 发布您的另一种方式解决方案。它将帮助 stackoverflow 社区。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2012-04-11
    • 2016-05-05
    • 1970-01-01
    • 2016-08-22
    相关资源
    最近更新 更多