美文网首页
iOS关于列表布局的几种实现方式小结

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

作者: 奔跑的小蚂蚁_8b28 | 来源:发表于2021-08-05 08:40 被阅读0次
见的列表布局样式 image

,功能的要求是最多六行,动态展示。当时想到的方案是,抽象出一个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关于列表布局的几种实现方式小结

    见的列表布局样式 ,功能的要求是最多六行,动态展示。当时想到的方案是,抽象出一个cell,初始化六个标签,动态的控...

  • masonry 源码解读

    ios 手写布局的几种方式 Frame AutoLayout VFL Masonry ios 布局的几种方式 1....

  • iOS开发之UITableView中计时器的几种实现方式

    iOS开发之UITableView中计时器的几种实现方式 iOS开发之UITableView中计时器的几种实现方式

  • css中常见的布局方式

    三栏布局 三栏布局是页面中比较常见的布局方式,也有好几种实现方式,分别是flex布局,grid布局,float布局...

  • 知识点之页面布局

    1. CSS圣杯布局的几种实现方式: 1. 浮动实现 2. 绝对定位实现 3. flex布局 4.表格布局...

  • iOS数据持久化

    ** iOS下数据持久化常用的几种方式:** NSUserDefaults plist(属性列表) NSKeyed...

  • CSS布局

    简单介绍几种CSS布局方式 左右布局 实现的方式: 1.float + margin: 2.position的ab...

  • 几种常见的CSS布局

    本文概要 本文将介绍如下几种常见的布局: 其中实现三栏布局有多种方式,本文着重介绍圣杯布局和双飞翼布局。另外几种可...

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • iOS 应用数据存储之XML(增、删、改)

    iOS应用数据存取的常用方式有如下几种XML属性列表 —— PListNSKeyedArchiver 归档Pref...

网友评论

      本文标题:iOS关于列表布局的几种实现方式小结

      本文链接:https://www.haomeiwen.com/subject/hexmvltx.html