美文网首页
iOS 选择商品规格

iOS 选择商品规格

作者: HH思無邪 | 来源:发表于2018-10-03 22:59 被阅读340次
选择商品规格.gif

由实现选规格的效果来记录下所用到的基础控件和知识点

控件: UIcollectionview

  1. 初始化
//初始化布局
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
 layout.scrollDirection=UICollectionViewScrollDirectionVertical;//竖直滚动
    layout.minimumInteritemSpacing=0;//itme左右间距
    layout.minimumLineSpacing=10;//itme 上下间距
//section 距上下左右的距离
    layout.sectionInset=UIEdgeInsetsMake(0, 10, 0, 10);
//头部的size
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 20);
    _collectionview.collectionViewLayout = layout;
    _collectionview.dataSource=self;
    _collectionview.delegate=self;
    _collectionview.backgroundColor=[UIColor whiteColor];
    //支持分页
    _collectionview.pagingEnabled=NO;
    _collectionview.scrollEnabled=NO;
    //注册cell
    [_collectionview registerNib:[UINib nibWithNibName:@"TSChooseSpeciColleCell" bundle:nil] forCellWithReuseIdentifier:@"TSChooseSpeciColleCell"];
    //注册组头
    UINib *headerNib = [UINib nibWithNibName:@"TSSpeciColleHead" bundle:nil];
    [_collectionview registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TSSpeciColleHead"];
  1. collectionView的代理和数据源方法
  • 有几组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.contentarry.count;
}
  • 每组有几个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.parammodel=self.contentarry[section];
    return self.parammodel.attr_values.count;
}

  • 加载自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   TSChooseSpeciColleCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"TSChooseSpeciColleCell" forIndexPath:indexPath];

   return cell;
}
  • 点击cell响应
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
  • 动态设置每个itme大小,根据文字字号大小自适应宽度。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    self.parammodel=self.contentarry[indexPath.section];
    self.valuemodel = [TSvaluesmodel mj_objectWithKeyValues: self.parammodel.attr_values[indexPath.row]];
    CGSize size = [NSString sizeWithText:self.valuemodel.attr_value font:[UIFont systemFontOfSize:20] maxW:kScreenWidth - 30];
            return size;
}
  • 计算文字返回size的方法
//根据文字和字体的大小计算文字的容器的大小
+ (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    //约束宽度
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
  • 加载自定义的collectview的Footer或者Header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//继承自UICollectionReusableView ,返回的要是这个类型才可以,和cell一样有重用机制

TSSpeciColleHead *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TSSpeciColleHead" forIndexPath:indexPath];
    self.parammodel=self.contentarry [indexPath.section];
    headerView.titleLab.text = self.parammodel.attr_name;
    return headerView;
}

实现多行单选效果

实现思路

  • 每一组只能选中一个,选择同行的会恢复上一次选中的状态,显示最新选中的cell的选中状态,每组不影响单独控制每一组,所以有几组就存几个状态值,要想要每每准确对应改变之前所选的值,那么就用字典的特性,用key找到它改掉它,成为最新的状态。

  • 实现: 用section当key 用row当value,存一个字典,改变选中的状态自动覆盖前一个值

NSString *rowstr =[NSString stringWithFormat:@"%ld",indexPath.row];
    NSString *sectstr =[NSString stringWithFormat:@"section%ld",indexPath.section];
    [self.Mudic setValue:rowstr forKey:sectstr];

相关文章

  • iOS 选择商品规格

    由实现选规格的效果来记录下所用到的基础控件和知识点 控件: UIcollectionview 初始化 collec...

  • ios开发-商品规格选择

    项目中有个商品规格选择如图:image.png 其实原理就是实现collectionview 的多section ...

  • iOS 商品规格选择View

    最近做了两三个商城类的项目,由于时间匆忙,商品规格选择的View都是网上找的Demo,用起来总归不是那么顺手,这两...

  • 质数在商品规格选择中的应用

    向淘宝京东商品选择的时候,会有规格选择,多种规格联动,如下三个截图淘宝的商品规格选择,在有的规格下没有商品或没货需...

  • iOS-商品详情规格选择

    前言:各大电商平台都有此类页面,在你选择要购买的商品时候,除了默认的商品规格外,你可以自由选择可供选择的规格,有的...

  • SKU商品规格选择

    在线demo地址 https://qdnzv.csb.app/[https://qdnzv.csb.app/] G...

  • 如何阻止元素的事件,默认以及非默认?

    应用场景:商品购物页面,在选择商品规格的时候,会根据上一个规格去判定其他规格是否还存在并可以组合到一起。 如下: ...

  • 分布式商城项目总结四之商品规格参数的实现

    2. 什么是商品规格参数 规格参数: 2.1.商品规格参数和类目关系 商品规格参数的模板是和商品类目关联的,不同的...

  • 商品规格

    京都产宇治抹茶味 薏仁 豆乳

  • 关于若干数组组合的问题 PHP

    主要是用于商品多规格时候 规格的组合,比如选择规格颜色:蓝色,红色,风格:卡通,动漫,在php 处理中要组合蓝色卡...

网友评论

      本文标题:iOS 选择商品规格

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