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

来源:互联网 发布:锤子科技 成都 知乎 编辑:程序博客网 时间:2024/05/22 16:54

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