美文网首页
UICollectionReusableView:行头行尾

UICollectionReusableView:行头行尾

作者: 你飞跃俊杰 | 来源:发表于2020-05-05 15:24 被阅读0次

    前言:以前用UICollectionView一行的时候,行头行尾不会出现任何问题
    当出现多行的时候,行头行尾的复用机制就开始坑爹了
    注册

    注册行头
    [self.collectView registerNib:[UINib nibWithNibName:@"NibName" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseIdentifier"];
    或者
       [self.collectView registerClass:[Class class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseIdentifier"]; 
    

    复用机制

    -(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        return headerView;
    }
    

    多行复用:于是苹果爸爸开始坑爹儿子(苹果:老子没你那么弱的儿子)

    一、Class

     [self.collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; 
    
            UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
            [headerView addSubview:view];
    
            NSString *text = (indexPath.section==0&&_model.theme_book_ids.count>0)?@"主题书籍":@"任务";
            UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(16, 25, 300, 20)];
            [lb setText:text textColor:[UIColor colorWithHexString:@"#303133"] fontSize:19];
            [view addSubview:lb];
    
            return headerView;
    
    1588663205403.jpg

    二、nib

     [self.collectView registerNib:[UINib nibWithNibName:@"LFThemeReadBooksHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
            LFThemeReadBooksHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
            headerView.frame = CGRectMake(0, 0, kScreenWidth, 45);
            headerView.label.text = (indexPath.section==0&&_model.theme_book_ids.count>0)?@"主题书籍":@"任务";
    return headerView;
    
    
    1588662923064.jpg 1eac4dc6d3bd5acd5c9f20443f89a3bd.jpg.gif 1588663205403.jpg

    解决方法:
    一、Class
    每次都清空之前的子控件,再加一遍 [headerView removeAllSubviews];

            UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
            [headerView removeAllSubviews];
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
            [headerView addSubview:view];
            
            NSString *text = (indexPath.section==0&&_model.theme_book_ids.count>0)?@"主题书籍":@"任务";
            UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(16, 25, 300, 20)];
            [lb setText:text textColor:[UIColor colorWithHexString:@"#303133"] fontSize:19];
            [view addSubview:lb];
    
            return headerView;
    

    二、nib
    注册两个nib 分别调用

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            if (indexPath.section == 0) {
                Type1HeaderView *type1HeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:Type1HeaderID forIndexPath:indexPath];
                return type1HeaderView;
            } else {
                Type2HeaderView *type2HeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:Type2HeaderID forIndexPath:indexPath];
                return type2HeaderView;
            }
        }
        return nil;
    }
    

    三、重写initwithframe方法

    -(instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
            _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, M_SIZE.width, M_SIZE.height / 14)];
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:_titleLabel];
        }
        return self;
    }
    

    爸爸已经告诉你了,是你个傻儿子不认真看


    1588663205403.jpg

    总结:
    你可以把

    • (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
      理解成
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      唯一不同的是UICollectionReusableView的复用机制比UITableViewCell的更蛋疼(比如用nib的时候)

    相关文章

      网友评论

          本文标题:UICollectionReusableView:行头行尾

          本文链接:https://www.haomeiwen.com/subject/mwmmghtx.html