开发中的生活iOS专题资源__UI专题fresh

iOS之商城应用中物品属性选择+购物车功能+订单确认

2016-04-06  本文已影响5807人  华之曦

本实例直接从当前项目中拉出来,实现的功能简单,但是在自己实现的时候用的代码老多多,而且当前只接收两组属性,不过属性实现了自动向下布局,超出屏幕可以上下滑动,但封装性不强,希望得到牛人指点。
下面简单介绍实现方法。


1.购物车属性选择功能

购物属性选择.gif
/**
 *  获取 商品属性 数据
 */
- (void)getGoodAttrData {
    NSString *URL = [NSString stringWithFormat:@"%@/App/Sylm/yclist",SERVERURL];
    NSMutableDictionary *paramas = [NSMutableDictionary dictionary];
    paramas[@"method"] = @"goods_attr";
    paramas[@"goods_id"] = @"11";
    [LXHttpTool post:URL params:paramas success:^(id json) {
        NSLog(@"%@", json);
        NSInteger status = [json[@"status"] integerValue];
        if (status == 1) {
            NSArray *goodAttrsArr = [GoodAttrModel mj_objectArrayWithKeyValuesArray:json[@"data"]];
            self.goodAttrsArr = goodAttrsArr;      
        } else {
            [SVProgressHUD showErrorWithStatus:@"暂无商品属性" maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请重新加载" maskType:SVProgressHUDMaskTypeGradient];
        NSLog(@"%@", error);
    }];
}
NSArray *attrValueArr0 = ((GoodAttrModel *)goodAttrsArr[0]).attr_value;
CGFloat one_btnsX = kBigMargin;
CGFloat one_btnY = CGRectGetMaxY(_firstAttributeLbl.frame) + kSmallMargin;
for (int i = 0; i < attrValueArr0.count ; i++) {
    NSString *btnTittle = ((GoodAttrValueModel *)attrValueArr0[i]).attr_value;
    CGSize size = [btnTittle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kButtonTextFont, NSFontAttributeName, nil]];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:btnTittle forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont boldSystemFontOfSize:13];
    btn.tag = i;
    [btn addTarget:self action:@selector(attrs1BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(one_btnsX, one_btnY, size.width + 30, 35);
    one_btnsX += kBigMargin + size.width + kBigMargin;

    while (one_btnsX  > kScreenW) {
        one_btnsX = kBigMargin;
        one_btnY += 45;
        if ((one_btnsX + size.width + 30) > kScreenW) {
            one_btnsX = kBigMargin;
            break;
        }
        btn.frame = CGRectMake(one_btnsX, one_btnY, size.width + 30, 35);
        one_btnsX += 15 + size.width + kBigMargin;
    }
    [btn defaultStyleWithNormalTitleColor:HX_RGB(136, 137, 138) andHighTitleColor:kWhiteColor andBorderColor:LXBorderColor andBackgroundColor:kWhiteColor andHighBgColor:kMAINCOLOR andSelectedBgColor:kMAINCOLOR withcornerRadius:2];
    [self.scrollView addSubview:btn];
    [self.firstBtnsArr addObject:btn];
}
@property (nonatomic, copy) void (^sureBtnsClick)(NSString *num, NSString *attr_id, NSString *goods_attr_value_1, NSString *goods_attr_value_2);

#pragma mark - 按钮点击事件
- (void)sureBtnClick {
    // 购买数量
    NSString *num = self.buyNumsLbl.text;
    // 属性ID str
    NSString *attr_id1 = [NSString stringWithFormat:@"%@-%@", _goods_attr_1, _goods_attr_value_1];
    NSString *attr_id2 = [NSString stringWithFormat:@"%@-%@", _goods_attr_2, _goods_attr_value_2];
    NSString *attr_id = [NSString stringWithFormat:@"%@|%@", attr_id1, attr_id2];
    if (self.sureBtnsClick) {
        self.sureBtnsClick(num, attr_id, self.goods_attr_value_1, self.goods_attr_value_2);
    }
    [self removeView];
}

2.购物车功能

3.订单确认页面

小结:回头研究下用SDAutoLayout来实现此种自动布局,最近一直在体验此框架。
令附上Demo地址--->传送门[Github],注:在此感谢朋友的阅读。

上一篇 下一篇

猜你喜欢

热点阅读