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

    问题产生 问题代码: List<String> strs = new LinkedList<String>(); // 中间有添加元素的操作,这里省略... // 这里toAr ...

  2. 三.mysql问答合集

    目录 三.MySQL 3.1 关系型和非关系型数据库的区别 关系型数据库 非关系型数据库 3.2 登录数据库的方式,如何远程登录 3.3 MySQL的服务结构,当客户端发起请求后,处理过程 3.4 如 ...

  3. 关于我升级VS16.8,结果一些项目运行报错“Phx.FatalError”这件事

    背景 不知道啥时候开始,一些的项目不能好好运行了.一运行就报错 解决办法 https://developercommunity.visualstudio.com/content/problem/125 ...

  4. 破局SAP实施难题、降低开发难度,定制化需求怎样快速上线?

    前言 SAP 是全球领先的业务流程管理软件供应商之一,其提供广泛的模块化解决方案和套件,所开发的软件解决方案面向各种规模的企业,帮助客户规划和设计业务流程.分析并高效设计整个价值链,以更好的了解和响应 ...

  5. conda 安装pytorch

    配置:win 10 ,python=3.6 安装pytorch-1.1.0,cudatoolkit-9.0,torchvision-0.3.0. 出现的问题:import torch 的时候,出现了O ...

  6. Python网页应用开发神器fac 0.3.0全新版本发布

    大家好我是费老师,在Python生态中,有很多以Python为主要开发语言,实现网页应用开发的框架,其中最为知名的有Dash.flet.streamlit.gradio.nicegui等. 如果综合考 ...

  7. Go 监控告警入门 Opentelemetry

    前言 Opentelemetry 分布式链路跟踪( Distributed Tracing )的概念最早是由 Google 提出来的,发展至今技术已经比较成熟,也是有一些协议标准可以参考.目前在 Tr ...

  8. AWG(American wire gauge)美国线规

    AWG(American wire gauge)美国线规,是一种区分导线直径的标准,又被称为 Brown & Sharpe线规.这种标准化线规系统于1857年起在美国开始使用.AWG前面的数值 ...

  9. VSCode 插件离线安装方法

    一.引言 最近想要使用 VSCode 来进行项目的开发工作,无奈工作机上无法上网.这就涉及到了相关插件的离线安装的问题. 在参考了 https://blog.csdn.net/wangwei703/a ...

  10. 一文搞懂 == 、equals和hashCode

    面试的时候,经常会被问到==和equals()的区别是什么?以及我们也知道重写equals()时候必须重新hashCode().这是为什么?既然有了hashCode()方法了,JDK又为什么要提供eq ...