iOS 开发 解决UICollectionView的多组头部视图样式不一样复用时发生错乱问题

本文探讨了在使用UICollectionView时遇到的多组头部视图样式复用导致的错乱问题。为了解决这个问题,关键在于根据头部视图的不同样式注册相应的视图,并在头部视图的代理方法中进行适当的条件判断处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UICollectionView用起来比UITableView麻烦多了,如何解决多组头部视图复用时出现的错乱问题就很关键

头部视图有几种样式就注册几种头部视图

    // 防止cell和头部视图复用出现错乱
    [collectionView registerClass:[WOCOHomeSelectTypeCell class] forCellWithReuseIdentifier:@"selectTypeCell"];
    [collectionView registerClass:[WOCOHomeDisplayCell class] forCellWithReuseIdentifier:@"displayCell"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerSelectType"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerDisplay"];

头部视图的代理方法中所做的判断处理

// 返回每一组的头部或尾部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    // 1.定义重用标识
    static NSString *ID;
    
    if (indexPath.section == 0) {
        ID = @"headerSelectType";
    } else {
        ID = @"headerDisplay";
    }
    
    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        // 网络加载 --- 创建带标题的图片轮播器
        
        // 防止复用时反复创建对象
        if (self.cycleScrollView == nil) {
            self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KUIScreenWidth, 180) delegate:self placeholderImage:nil];
            
            self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
            self.cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
            [reusableView addSubview:self.cycleScrollView];
            
            // --- 模拟加载延迟
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.cycleScrollView.imageURLStringsGroup = self.imagesURLStringArr;
            });
        }

    } else {
        // 防止复用时反复创建对象
        if (self.lineView == nil) {
            UIImageView *lineView = [[UIImageView alloc] init];
            lineView.backgroundColor = [UIColor blackColor];
            self.lineView = lineView;
            
            [reusableView addSubview:lineView];
            
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(reusableView.mas_centerY);
                make.left.mas_equalTo(reusableView.mas_left).offset(10);
                make.height.mas_equalTo(20);
                make.width.mas_equalTo(3);
            }];
        }
        // 防止复用时反复创建对象
        if (self.tipLabel == nil) {
            UILabel *tipLabel = [[UILabel alloc] init];
            tipLabel.text = @"定制精选";
            tipLabel.textColor = [UIColor blackColor];
            self.tipLabel = tipLabel;
            
            [reusableView addSubview:tipLabel];
            
            [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(reusableView.mas_centerY);
                make.left.mas_equalTo(self.lineView.mas_right).offset(10);
            }];
        }
    }
    return reusableView;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值