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;
}
有时候一个思路就是一种方案。代码确实优化了不少,长知识啊。