iOS 选择商品规格

2018-10-03  本文已影响340人  HH思無邪
选择商品规格.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;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.parammodel=self.contentarry[section];
    return self.parammodel.attr_values.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   TSChooseSpeciColleCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"TSChooseSpeciColleCell" forIndexPath:indexPath];

   return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
- (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;
}
//根据文字和字体的大小计算文字的容器的大小
+ (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;
}
- (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;
}

实现多行单选效果

实现思路

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

猜你喜欢

热点阅读