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

    在前端Vue项目中,接口会被统一放在一个目录中管理: 一个模块的所有接口放在一个JS文件中: 文件会导入封装好的请求方法,和动态绑定的接口地址 import request from '@/utils ...

  2. PVE linux_VM 扩容分区

    页面 调整磁盘大小 手动分区 fdisk -l fdisk /dev/sda 对该磁盘进行分区, 输入n并回车,n是"new"新建分区 [root@localhost ~]# fd ...

  3. pyqt编写的走迷宫游戏环境,python语言,exe文件可以成功运行Window10系统上

    PS. 要注意,这个项目使用源码安装的方式无法成功运行,显示报错,尝试过多个python版本和pyqt的版本,估计是长期没有维护的原因,因此源码是无法运行的,但是exe的可执行文件(打包后的)是可以成 ...

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

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

  5. 飞书Webhook触发操作指南,实现事件驱动型工作流自动化

    本文提供了利用数据触发Feishu Webhook的具体操作指南,包括Webhook的设置以及编写触发代码的方法,为读者提供了实践参考,希望能帮助解决你目前遇到的问题. 描述 用于使用数据触发 Fei ...

  6. flex布局被内容被撑开及flex布局下定宽元素被压缩

    实现效果使用flex进行左右布局,左边定宽200px,右边自适应,当右边内容过多,造成右边盒子被撑开,会造成两种问题 左边定宽盒子被压缩解决办法: flex-grow:0;//是否自动增长空间 fle ...

  7. java关于数组的复制,反转、查找

    一.数组的赋值: arr2=arr1;对于该赋值而言,地址值一样,所以arr1会随着arr2的变化而变化.这不能称作数组的复制,因为只是把地址赋过去了.地址一样,指向的是堆空间中唯一的数组实体(数值) ...

  8. 利用Stream实现简单的等差数列求和

    我们都熟知高斯的故事,认识等差数列也是从这个故事开始的,编程课程为了练习for循环,也在不断的练习这个从1加到100的例子,那么原始的办法是这样的: int sum1 = 0; for (int i ...

  9. 将整个工程的GBK转为utf-8格式

    eclipse将整个工程转为utf-8时原先中文注释会变为乱码,13年时写了个脚本将整个文件的java以及配置文件转为utf-8格式,下面是代码 package com.code.pd; import ...

  10. API 接口是什么?怎么对接 API?

    API接口是预先定义的函数,允许应用间共享数据和功能.对接API涉及获取接口文档,通过POST请求调用如http://域名地址/queryLoginWx的URL,使用特定Headers.成功返回会包含 ...