美文网首页
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:行头行尾

    前言:以前用UICollectionView一行的时候,行头行尾不会出现任何问题当出现多行的时候,行头行尾的复用机...

  • WebStorm学习HTML杂记

    option +拖拽鼠标:可以同时输入多行command + 左右键: 可以跳到行头行尾command+d:快速复...

  • UICollectionReusableView 头视图注册

    - (UICollectionReusableView *)collectionView:(UICollectio...

  • 设置 Android Studio 支持光标前后左右快捷键

    以前用 Xcode,为了提升编码速度,不让手指从字母区移到方向区,强行用快捷键来操控光标的上下左右及移到行头和行尾...

  • 带sectionHeaderView的UICollectionV

    headerView继承UICollectionReusableView实现CHTCollectionViewDe...

  • Linux vim命令

    一般模式 删除,撤销,复制,复制几行,粘贴,删除一个字母(2),删除一个单词,复制一个单词,移动到行头,移动到行尾...

  • UICollectionReusableView 使用

    最近遇到一个很奇葩的bug,collection头部没有被释放,但是在12.0以下的机型会消失,在xr 手机 14...

  • UICollectionView 设置headView

    从 UICollectionReusableView 继承。如 然后在Vc里面通过registerClass把该类...

  • 《行头》

    作者~美丽 我不恋红尘 我不恋城市套路 我愿带着行头出走 画上自己喜欢的戏剧脸谱 即便是唱着情歌流着泪 我也愿意 ...

  • 行头

    衣服 曾经有很长的时间没有逛街的欲望,原因是无论走进哪一家店铺,都挑不到自己...

网友评论

      本文标题:UICollectionReusableView:行头行尾

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