当前位置: 首页 > news >正文

iOS关于列表布局的几种实现方式小结

,功能的要求是最多六行,动态展示。当时想到的方案是,抽象出一个cell,初始化六个标签,动态的控制显示和隐藏,这样功能上没有问题,就是代码有些冗余。请教了身边的美女同事,她那边的思路是用UICollectionView来布局实现。经过优化后的代码如下。

  

- (void)setupUI{

    

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];

    layout.itemSize = CGSizeMake(K_CC_SCREEN_WIDTH, 20+10);

  //行间距

    layout.minimumLineSpacing = 0;

  //列间距

    layout.minimumInteritemSpacing = 0;

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];

    [self.contentView addSubview:self.collectionView];

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    self.collectionView.layer.masksToBounds = YES;

    self.collectionView.layer.cornerRadius = 8.0;

    self.collectionView.showsVerticalScrollIndicator = NO;

    self.collectionView.showsHorizontalScrollIndicator = NO;

    self.collectionView.backgroundColor = [UIColor clearColor];

    [self.collectionView registerClass:[CCHighSeasPoolCenterCollectionViewCell class] forCellWithReuseIdentifier:ccHighSeasPoolCenterCollectionViewCellIdentifier];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataSeasLeftList.count;

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    CCHighSeasPoolCenterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ccHighSeasPoolCenterCollectionViewCellIdentifier forIndexPath:indexPath];

    

    CCHighSeasPoolFieldListModel *leftDic=[self.dataSeasLeftList objectAtIndex:indexPath.item];

    //此处第一行展示一个值,其余列表需要拼接字段

    NSString *firstText=[NSString stringWithFormat:@"%@:%@",[self getNewValue:leftDic.nameLabel],[self getNewValue:[self.itemDic objectForKey:[self getNewKey:leftDic]]]];

    if (indexPath.item==0) {

        //此处需要单独设置第一行的样式

        [cell.lblType setText:[self getNewValue:[self.itemDic objectForKey:[self getNewKey:leftDic]]]];

        cell.lblType.textColor = K_CC_COLOR_STRING(@"#333333");

        cell.lblType.font = [UIFont boldSystemFontOfSize:16];

    }else{

        [cell.lblType setText:firstText];

        cell.lblType.textColor = K_CC_COLOR_STRING(@"#999999");

        cell.lblType.font = [UIFont systemFontOfSize:14];

    }

    

    return cell;

}

- (void)setDic:(NSMutableDictionary *)dic{

    //此处获取当前字典的值,在cell列表中使用

    self.itemDic=dic;

    

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(0);

        make.top.mas_equalTo(self.contentView.mas_top);

        make.width.mas_equalTo(K_CC_SCREEN_WIDTH);

        make.bottom.mas_equalTo(self.contentView.mas_bottom);

    }];

    

    [self.imageSelect mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(16);

        make.top.mas_equalTo(14);

        make.size.mas_equalTo(CGSizeMake(21, 21));

    }];

    

    [self.btnSelect mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.mas_equalTo(80);

        make.height.mas_equalTo(80);

        make.top.mas_equalTo(0);

        make.left.mas_equalTo(0);}];

    

    [self.btnJump mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.mas_equalTo(K_CC_SCREEN_WIDTH-80);

        make.bottom.mas_equalTo(self.contentView.mas_bottom);

        make.top.mas_equalTo(0);

        make.left.mas_equalTo(45);}];

    

    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(0);

        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-1);

        make.size.mas_equalTo(CGSizeMake(K_CC_SCREEN_WIDTH, 1));

    }];

    

    [self.collectionView reloadData];

}

//获取新的key

-(NSString *)getNewKey:(CCHighSeasPoolFieldListModel*)temDic

{

    NSString *tempKey=temDic.schemefieldName;

    

    NSString *schemefieldType=temDic.schemefieldType;

    //IMG

    if ([schemefieldType isEqualToString:@"Y"]||[schemefieldType isEqualToString:@"M"]||[schemefieldType isEqualToString:@"R"]||[schemefieldType isEqualToString:@"MR"]||[schemefieldType isEqualToString:@"FL"]||[schemefieldType isEqualToString:@"FL"]) {

        //如果是这几种情况,需要拼接ccname

        tempKey=[NSString stringWithFormat:@"%@ccname", tempKey];

    }

    return tempKey;

}

//获取新的value

-(NSString *)getNewValue:(NSString*)temValue

{

    if ([CCCommonAPI xfunc_check_strEmpty:temValue] ) {

        temValue=@"";

    }

    return temValue;

}

  有时候一个思路就是一种方案。代码确实优化了不少,长知识啊。

 

相关文章:

  • python 2024-9
  • TCL一面(HR)
  • golang学习笔记11——Go 语言的并发与同步实现详解
  • sqlite在Windows环境下安装、使用、node.js连接
  • Python(PyTorch和TensorFlow)图像分割卷积网络导图(生物医学)
  • 中国《人工智能安全治理框架》1.0版正式发布 规范各类AI、算法
  • Vant Weapp
  • Qt6.8 GRPC功能使用(2)标准 Qt实现客户端
  • ChatGPT带火的HBM是什么?
  • Redis实现滑动窗口限流
  • 远程控制桌面,让电脑办公更简单
  • sql-labs第46关 order by盲注
  • 如何让Springboot RestTemplate同时支持发送HTTP及HTTPS请求呢?
  • Linux之(15)shell(7)常见命令
  • Django(8):请求对象和响应对象
  • 汽车安全气囊设计?Abaqus/Part特殊建模方法-附案例step-by-step教学
  • G1垃圾回收器
  • 2022/11/21[指针] 多维数组与指针的联系
  • 基于STM32 CubeMX利用RTC实现带温湿度模块的万年历
  • 第十一章 Golang面向对象编程(下)
  • ISO质量管理体系认证有什么优势
  • 双软认证需要的条件
  • WPF动画(2)
  • UE4 回合游戏项目 22- 控制新角色
  • RemObjects SDK for Delphi
  • 通过实战总结的 使用GoFrame小技巧
  • Python 字符串详解
  • MATLAB改变默认工作路径
  • 如何找回删除的文件?数据恢复,看这几个方法
  • 从三层架构说起,谈谈对历史项目的小改造
  • wordpress的手工迁移
  • wy的leetcode刷题记录_Day47