最近在项目开发中,遇到了常见的列表布局样式,功能的要求是最多六行,动态展示。当时想到的方案是,抽象出一个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;

}

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

iOS关于列表布局的几种实现方式小结的更多相关文章

  1. Android学习系列(二)布局管理器之线性布局的3种实现方式

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39643669 LinearLayout是Android控件中的线性布局控件,它包括的子控件 ...

  2. css布局 - 垂直居中布局的一百种实现方式(更新中...)

    首先将垂直居中的现象和实现方式两大方向细分类如下: 接下来逐条累加不同情况下的垂直居中实现. 目录: 一.父元素高度固定时,单行文本 | 图片的垂直居中 1. line-height行高简单粗暴实现法 ...

  3. css左右布局的几种实现方式和优缺点

    记录一下左右布局的实现方式,实现的具体效果是,左侧固定宽度,高度适中等于父元素的高度,父元素的高度由右侧内容决定: html代码如下: <div class="parent" ...

  4. CSS布局的四种定位方式

    1.static(静态定位): 默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明).参考上篇随笔. 2.relative(相对 ...

  5. js创建对象的几种常用方式小结

    第一种模式:工厂方式  var lev=function(){ return "666"; }; function Parent(){ var Child = new Object ...

  6. css布局 - 两栏自适应布局的几种实现方法汇总

    这种两列布局的样式是我们在平时工作中非常常见的设计,同时也是面试中要求实现的高频题.很有必要掌握以备不时之需: 整理了几种实现方法,当然,风骚的代码不止这几种能实现,欢迎各位的补充. 方法汇总目录 简 ...

  7. 一个可变布局列表,有9种布局item大小,每个item可拖拽切换位置

    代码地址如下:http://www.demodashi.com/demo/11271.html 一.准备工作 准备一台安卓设备手机,4.4以上版本 本例子实现,一个可变布局列表,有9种布局item大小 ...

  8. 实现顶部轮播,下部listview经典布局的两种方式

    开头: 在做android开发的时候,我们经常会遇到这样的布局,上面是一个图片轮播图,下面是一些列表的项目.很多新闻app,视频类app都采用这样的布局.起初的时候 由于没有很多参考,我自己想到了一种 ...

  9. iOS: 属性列表介绍 Introduction to Property Lists

    iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...

  10. iOS 之 线性布局

    本来想自己写一个线性布局的类,看来不用了 ,网上已经有了,我先试试好不好用. https://github.com/youngsoft/MyLinearLayout 线性布局MyLinearLayou ...

随机推荐

  1. 【Nexus】Linux上的Maven私服搭建

    [1.安装Nexus] 需要Linux安装JDK运行,Nexus2版本JDK7,3版本JDK8 首先需要Nexus服务器文件 nexus-2.12.0-01-bundle.tar.gz 解压 tar ...

  2. 实现一个终端文本编辑器来学习golang语言

    欢迎!这个系列的博文会带你使用golang语言来编写一个你自己的文本编辑器. 首先想说说写这个系列文章的动机. 其实作为校招生加入某头部互联网大厂一转眼已经快4年了.可以说该大厂算是比较早的用gola ...

  3. 支持AMD GPU —— 如何运行docker环境下的Jax环境

    相关: 支持NVIDIA GPU -- 如何运行docker环境下的Jax环境 官方给出的安装主页: https://hub.docker.com/r/rocm/jax 安装命令: docker pu ...

  4. pytorch报错----------- ***ValueError: some of the strides of a given numpy array are negative.

    最近遇到的一个pytorch报错: 然后报错了,这个几行代码就是从一个图片中读入数据,把bgr模式图片矩阵转换为rgb模式,这里采用的是改变矩阵索引,索引倒排     [..., ::-1]   . ...

  5. 网络问题解决:Ubuntu连接局域网中Windows主机上的v2r报错:rejected core/proxy/socks: unknown Socks version: 67

    参考: https://github.com/2dust/v2rayN/issues/3916 https://www.eolink.com/news/post/30941.html ======== ...

  6. (续)MindSpore计算框架如何发布训练好的模型到官方模型仓库MindSpore_Hub上 —— 对fork的MindSpore_Hub进行PR提交

    参考: https://gitee.com/mindspore/mindspore/blob/r1.2/CONTRIBUTING.md ================================ ...

  7. Ubuntu系统anaconda报错version `GLIBCXX_3.4.30' not found

    参考文章: https://blog.csdn.net/zhu_charles/article/details/75914060 =================================== ...

  8. Ambiguous Mapping 的处理方法

    出现原因:两个不同的Controller 中出现相同映射路径 eg: AController:/v1/wdnmd/userList/list BController:/v1/wdnmd/userLis ...

  9. 处理报错 ResizeObserver loop completed with undelivered notifications.

    // 处理报错 ResizeObserver loop completed with undelivered notifications. export const handlerResizeObse ...

  10. CASIO fx-991CN X 使用

    复数转换 \(a+b \, {\mathrm i} \leftrightarrow r \angle \theta\) 进入复数运算模式 菜单 2 输入待转换数 OPTN ▼,选择目标格式. = 可通 ...