iOS_一個購物車的使用

来源:http://www.cnblogs.com/blogwithstudyofwyn/archive/2016/06/26/5618107.html
-Advertisement-
Play Games

這個項目是本人原創:要轉載,請說明下:http://www.cnblogs.com/blogwithstudyofwyn/p/5618107.html 項目的地址:https://github.com/Shangshanroushui/ShoppingCart.git 該程式是個一元奪寶的的購物車。 ...


這個項目是本人原創:要轉載,請說明下:http://www.cnblogs.com/blogwithstudyofwyn/p/5618107.html

項目的地址:https://github.com/Shangshanroushui/ShoppingCart.git

該程式是個一元奪寶的的購物車。

項目中依然使用的MVC

model:是商品的全部信息

GoodsInfoModel.h

@property(strong,nonatomic)NSString *imageName;//商品圖片
@property(strong,nonatomic)NSString *goodsTitle;//商品標題
@property(strong,nonatomic)NSString *goodsPrice;//商品單價
@property(assign,nonatomic)BOOL selectState;//是否選中狀態
@property(assign,nonatomic)NSInteger goodsNum;//商品個數
@property(assign,nonatomic)NSInteger allNum;//全部個數
@property(assign,nonatomic)NSInteger remainedNum;//還需個數
-(instancetype)initWithDict:(NSDictionary *)dict;

GoodsInfoModel.m

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        self.imageName = dict[@"imageName"];
        self.goodsTitle = dict[@"goodsTitle"];
        self.goodsPrice = dict[@"goodsPrice"];
        self.goodsNum = [dict[@"goodsNum"]integerValue];
        self.selectState = [dict[@"selectState"]boolValue];
        self.allNum=[dict[@"allNum"]integerValue];
        self.remainedNum=[dict[@"remainedNum"]integerValue];        
    }
    
    return  self;
}

View :1.自定義的一個tableViewCell 2.自定義的一個結算View

ShopCartCell.h  :1.用於展示內容的各種控制項 2.自定義了一個協議和代理 3.一個按鈕觸發事件

PS:英文使用了masonry 自動佈局。將需要的一些頭文件和巨集定義放到pch文件中了。直接導入pch文件了

#import "GoodsInfoModel.h"

//添加代理,用於按鈕加減的實現
@protocol ShopCartCellDelegate <NSObject>
-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag;
@end
@interface ShopCartCell : UITableViewCell
//增加一個view
@property (nonatomic,strong) UIView *mainView;
@property (nonatomic,strong) UIButton *selectBtn;
@property (nonatomic,strong) UIImageView *goodsImg;
@property (nonatomic,strong) UILabel *introductionLab;
@property (nonatomic,strong) UILabel *needLab;
@property (nonatomic,strong) UILabel *needNumLab;
@property (nonatomic,strong) UILabel *remainedLab;
@property (nonatomic,strong) UILabel *remianedNumLab;
@property (nonatomic,strong) UIButton *addBtn;
@property (nonatomic,strong) UITextField *goodsNumTF;
@property (nonatomic,strong) UIButton *minusBtn;
@property (nonatomic,strong) UILabel *winLab;
@property (nonatomic,strong) UILabel *winNumLab;
@property(assign,nonatomic)BOOL selectState;//選中狀態
//賦值
-(void)addTheValue:(GoodsInfoModel *)goodsModel;
@property(assign,nonatomic)id<ShopCartCellDelegate>delegate;

ShopCartCell.m


 #import "SC.pch"

@interface ShopCartCell()

@end
@implementation ShopCartCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.layer.borderColor = [UIColor redColor].CGColor;
//        self.layer.borderWidth = 1;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
//        self.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:0.5];
//        [self setSeparatorInset:UIEdgeInsetsMake(0, 60, 0, 0)];
        UIView *mainView=[[UIView alloc]init];
        [self.contentView addSubview:mainView];
        mainView.backgroundColor=[UIColor whiteColor];
        self.mainView=mainView;
        
        UIButton *selectBtn = [[UIButton alloc]init];
        [self.mainView addSubview:selectBtn];
        [selectBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
        [selectBtn setImage:[UIImage imageNamed:@"5"]forState:UIControlStateSelected];
        [selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        selectBtn.tag=13;
        self.selectBtn=selectBtn;

        
        UIImageView *goodsImg = [[UIImageView alloc]init];
        [self.mainView addSubview:goodsImg];
//        goodsImg.image = [UIImage imageNamed:@"4"];
        self.goodsImg=goodsImg;
        
        UILabel *introductionLab = [[UILabel alloc]init];
        [introductionLab setFont:[UIFont systemFontOfSize:17.0]];
        [self.mainView addSubview:introductionLab];
//        introductionLab.text=@"[111111111]";
        introductionLab.textColor=[UIColor grayColor];
        self.introductionLab=introductionLab;
        
        UILabel *needLab = [[UILabel alloc]init];
        [needLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:needLab];
        needLab.text=@"總需:";
        needLab.textColor=[UIColor grayColor];
        self.needLab=needLab;
        
        
        UILabel *needNumLab = [[UILabel alloc]init];
        [needNumLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:needNumLab];
//        needNumLab.text=@"1000";
        needNumLab.textColor=[UIColor grayColor];
        self.needNumLab=needNumLab;
        
        
        UILabel *remainedLab = [[UILabel alloc]init];
        [remainedLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:remainedLab];
        remainedLab.textColor=[UIColor grayColor];
        remainedLab.text=@"剩餘:";
        self.remainedLab=remainedLab;
        
        UILabel *remianedNumLab = [[UILabel alloc]init];
        [remianedNumLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:remianedNumLab];
//        remianedNumLab.text=@"999";
        remianedNumLab.textColor=[UIColor redColor];
        self.remianedNumLab=remianedNumLab;
        
        
        UIButton *minusBtn = [[UIButton alloc]init];
        [self.mainView addSubview:minusBtn];
        [minusBtn setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
        [minusBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        minusBtn.tag = 11;
        self.minusBtn=minusBtn;
        
        UITextField *goodsNumTF = [[UITextField alloc]init];
        [self.mainView addSubview:goodsNumTF];
        goodsNumTF.textAlignment=NSTextAlignmentCenter;
//        goodsNumTF.text=@"10";
        goodsNumTF.backgroundColor=[UIColor grayColor];
        self.goodsNumTF=goodsNumTF;
        
        UIButton *addBtn = [[UIButton alloc]init];
        [self.mainView addSubview:addBtn];
        [addBtn setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        addBtn.tag = 12;
        self.addBtn=addBtn;

        
        UILabel *winLab = [[UILabel alloc]init];
        [winLab setFont:[UIFont systemFontOfSize:14.0]];
        [self.mainView addSubview:winLab];
        winLab.text=@"中獎概率";
        winLab.textColor=[UIColor grayColor];
        self.winLab=winLab;
        
        UILabel *winNumLab = [[UILabel alloc]init];
        [winNumLab setFont:[UIFont systemFontOfSize:14.0]];
        [self.mainView addSubview:winNumLab];
        winNumLab.text=@"100%";
        winNumLab.textColor=[UIColor grayColor];
        self.winNumLab=winNumLab;
        
    }
    return self;
}
/**
 
 *  給單元格賦值
 
 *
 
 *  @param goodsModel 裡面存放各個控制項需要的數值
 
 */

-(void)addTheValue:(GoodsInfoModel *)goodsModel
{
    self.goodsImg.image = [UIImage imageNamed:goodsModel.imageName];
    self.introductionLab.text = goodsModel.goodsTitle;
    self.needNumLab.text = [NSString stringWithFormat:@"%ld",(long)goodsModel.allNum];
    self.remianedNumLab.text = [NSString stringWithFormat:@"%ld",(long)goodsModel.remainedNum];
    self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsModel.goodsNum];

    if (goodsModel.selectState){
        self.selectState = YES;
        [self.selectBtn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
    }else{
        
         self.selectState = NO;
        [self.selectBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    }
}
//
-(void)selectBtnAction:(UIButton *)sender{

    [self.delegate btnClick:self andFlag:(int)sender.tag];
}
//
-(void)deleteBtnAction:(UIButton *)sender{
//    NSInteger goodsNum=[self.goodsNumTF.text integerValue];
//    if (goodsNum>1) {
//        goodsNum-=1;
//        self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsNum];
//        NSInteger remianedNum=[self.remianedNumLab.text integerValue]+1;
//        self.remianedNumLab.text=[NSString stringWithFormat:@"%ld",(long)remianedNum];
//    }
    ////////
    //判斷是否選中,選中才能點擊
    if (self.selectState == YES)
    {
        //調用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }
}

-(void)addBtnAction:(UIButton *)sender{
//    NSInteger goodsNum=[self.goodsNumTF.text integerValue];
//    if ([self.remianedNumLab.text integerValue]>0) {
//        goodsNum+=1;
//        self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsNum];
//        NSInteger remianedNum=[self.remianedNumLab.text integerValue]-1;
//        self.remianedNumLab.text=[NSString stringWithFormat:@"%ld",(long)remianedNum];
//    }
    ///////
    if (self.selectState == YES)
    {
        //調用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    WEAKSELF(weakSelf);
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(weakSelf).offset(0);
        make.left.and.right.mas_equalTo(0);
        make.top.and.bottom.mas_equalTo(weakSelf).offset(10);
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mainView.mas_centerY).offset(0);
        make.left.mas_equalTo(10);
        //make.height.and.width.mas_equalTo(17);
        make.height.mas_equalTo(17);
        make.width.mas_equalTo(17);
    }];
    
    [self.goodsImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mainView.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf.selectBtn.mas_right).offset(10);
        //make.height.and.width.mas_equalTo(17);
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(70);
    }];
    
    [self.introductionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mainView.mas_top).offset(10);
        make.left.mas_equalTo(weakSelf.goodsImg.mas_right).offset(10);
        make.right.mas_equalTo(weakSelf.mas_right).offset(0);
        make.height.mas_equalTo(20);
    }];

    [self.needLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.introductionLab.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.introductionLab).offset(0);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];

    [self.needNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.needLab.mas_right).offset(0);
        make.height.mas_equalTo(weakSelf.needLab.mas_height).offset(0);
        make.width.mas_equalTo(60);
    }];

    [self.remainedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.needNumLab.mas_right).offset(10);
        make.height.mas_equalTo(weakSelf.needLab.mas_height);
        make.width.mas_equalTo(40);
    }];

    [self.remianedNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.remainedLab.mas_right).offset(0);
        make.height.mas_equalTo(weakSelf.needLab.mas_height);
        make.width.mas_equalTo(60);
    }];
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.introductionLab).offset(0);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(24);
    }];
    ////////////////
    
    [self.winLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.mas_right).offset(-50);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(60);
    }];
    
    [self.winNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.winLab.mas_bottom).offset(5);
        make.right.mas_equalTo(weakSelf.winLab).offset(0);
        make.height.mas_equalTo(weakSelf.winLab);
        make.width.mas_equalTo(weakSelf.winLab);
    }];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.winLab.mas_left).offset(-20);
        make.height.mas_equalTo(weakSelf.minusBtn);
        make.width.mas_equalTo(weakSelf.minusBtn);
    }];

    [self.goodsNumTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.addBtn.mas_left).offset(-10);
        make.left.mas_equalTo(weakSelf.minusBtn.mas_right).offset(10);
        make.height.mas_equalTo(weakSelf.minusBtn);
    }];
}

 

SettlementView.h  一個結算的view ,用於顯示選擇的個數和金額,以及里那個按鈕:全選和結算按鈕。

@interface SettlementView : UIView
@property(nonatomic,strong)UIButton*allSelecteBtn;
@property(nonatomic,strong)UILabel*allSelecteLab;//全選
@property(nonatomic,strong)UILabel*sumLab;//總價
@property(nonatomic,strong)UILabel*goodsNumLab;//商品數
@property(nonatomic,strong)UIButton *statementBtn;//結算
@end

SettlementView.m 

#import "SC.pch"
@implementation SettlementView

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self){
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    self.backgroundColor=[UIColor whiteColor];
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 0.5;
    //全選按鈕
    //self.allSelecteBtn
    UIButton *allSelecteBtn=[[UIButton alloc]init];
    [self addSubview:allSelecteBtn];
    [allSelecteBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _allSelecteBtn=allSelecteBtn;
    
    [self.allSelecteBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//    [self.allSelecteBtn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateSelected];

    self.allSelecteLab=[[UILabel alloc]init];
    [self addSubview:self.allSelecteLab];
    self.allSelecteLab.text=@"全選";
    self.allSelecteLab.font=[UIFont systemFontOfSize:14.0];
    
    self.sumLab=[[UILabel alloc]init];
    [self addSubview:self.sumLab];
    self.sumLab.text=@"0 元";
    self.sumLab.textColor=[UIColor redColor];
    self.allSelecteLab.font=[UIFont systemFontOfSize:16.0];
    
    self.goodsNumLab=[[UILabel alloc]init];
    [self addSubview:self.goodsNumLab];
    self.goodsNumLab.text=@"共計:0 件商品";
    self.goodsNumLab.font=[UIFont systemFontOfSize:12.0];
    
    //全選按鈕
    self.statementBtn=[[UIButton alloc]init];
    [self addSubview:self.statementBtn];
    [self.statementBtn setBackgroundColor:[UIColor redColor]];
    [self.statementBtn setTitle:@"結算" forState:UIControlStateNormal];    
}
-(void)selectBtnAction:(UIButton *)sender{
    
    //[self.delegate btnClick:self andFlag:(int)sender.tag];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    WEAKSELF(weakSelf);
    [self.allSelecteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf).offset(10);
        make.height.mas_equalTo(17);
        make.width.mas_equalTo(17);
    }];

    [self.allSelecteLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf.allSelecteBtn.mas_right).offset(5);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    [self.sumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(weakSelf.allSelecteLab.mas_right).offset(20);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(100);
    }];
    [self.goodsNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.sumLab.mas_bottom).offset(5);
        make.left.mas_equalTo(weakSelf.sumLab);
        make.height.mas_equalTo(weakSelf.sumLab);
        make.width.mas_equalTo(weakSelf.sumLab);
    }];
    [self.statementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.right.mas_equalTo(weakSelf.mas_right).offset(-5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
}

 

Controller:

ShopCartViewController.h

ShopCartViewController.m  

項目最核心:選擇商品,添加,減少等,以及在這個操作中結算View的變化。

#import "ShopCartViewController.h"
#import "ShopCartCell.h"
#import "SC.pch"
#import "SettlementView.h"
#import "GoodsInfoModel.h"

#import "StatementViewController.h"
@interface ShopCartViewController ()<UITableViewDataSource,UITableViewDelegate,ShopCartCellDelegate>
@property(nonatomic,strong)UITableView *goodsTableView;
@property(nonatomic,strong)SettlementView *settlementView;
@property(nonatomic,assign)NSInteger allPrice;
@property(nonatomic,assign)NSInteger goodsNum;;
@property(nonatomic,strong)NSMutableArray *infoArr;;
@end
@implementation ShopCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
    [self setInfo];
    [self setGoodsTableView];
    [self setupSettlement];

}
-(void)setInfo{
    self.allPrice=0;
    self.goodsNum = 0;
    self.infoArr = [[NSMutableArray alloc]init];
    /**
     
     *  初始化一個數組,數組裡面放字典。字典裡面放的是單元格需要展示的數據
     
     */
    
    for (int i = 0; i<7; i++)
        
    {
        
        NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
        
        [infoDict setValue:@"4.png" forKey:@"imageName"];
        
        [infoDict setValue:@"這是商品標題" forKey:@"goodsTitle"];
        
//        [infoDict setValue:@"2000" forKey:@"goodsPrice"];
        
        [infoDict setValue:[NSNumber numberWithBool:NO] forKey:@"selectState"];
        
        [infoDict setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];
        /*
         @property(assign,nonatomic)int allNum;
         
         @property(assign,nonatomic)int remainedNum;
         */
        [infoDict setValue:[NSNumber numberWithInt:1000] forKey:@"allNum"];
        [infoDict setValue:[NSNumber numberWithInt:999] forKey:@"remainedNum"];
        
        //封裝數據模型
        GoodsInfoModel *goodsModel = [[GoodsInfoModel alloc]initWithDict:infoDict];
        
        //將數據模型放入數組中
        
        [self.infoArr addObject:goodsModel];
        
    }
}
-(void)setupSettlement{
    SettlementView *settlementView=[[SettlementView alloc]initWithFrame:CGRectMake(0, kScreen_Height-100, kScreen_Width, 60)];
    [settlementView.statementBtn addTarget:self action:@selector(goStatement) forControlEvents:UIControlEventTouchUpInside];
    
    [settlementView.allSelecteBtn addTarget:self action:@selector(goAllSelect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:settlementView];
    self.settlementView=settlementView;
    
    
}
-(void)setGoodsTableView{
    UITableView *goodsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-20)];//0, 44, kScreen_Width, kScreen_Height-20)
    [self.view addSubview:goodsTableView];
    goodsTableView.showsHorizontalScrollIndicator=NO;
    goodsTableView.showsVerticalScrollIndicator=NO;
//    goodsTableView.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
    goodsTableView.backgroundColor=[UIColor redColor];
    goodsTableView.delegate=self;
    goodsTableView.dataSource=self;
    [goodsTableView registerClass:[ShopCartCell class] forCellReuseIdentifier:@"SCCell"];
//    [goodsTableView registerNib:[UINib nibWithNibName:@"ShopCartTableCell" bundle:nil] forCellReuseIdentifier:@"SCCell"];
    goodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.goodsTableView=goodsTableView;
}

#pragma mark-------
#pragma mark--------tableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *SCCell =  @"SCCell";
    ShopCartCell *cell=[tableView dequeueReusableCellWithIdentifier:SCCell forIndexPath:indexPath];
    if (!cell){
        cell=[[ShopCartCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:SCCell];
    }
    cell.delegate = self;
    [cell addTheValue:self.infoArr[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArr.count;
}

#pragma mark -- 實現加減按鈕點擊代理事件
/**
 *  實現加減按鈕點擊代理事件
 *
 *  @param cell 當前單元格
 *  @param flag 按鈕標識,11 為減按鈕,12為加按鈕
 */
-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag{
    NSIndexPath *index = [self.goodsTableView indexPathForCell:cell];
    switch (flag) {
        case 11:
        {
            //做減法
            //先獲取到當期行數據源內容,改變數據源內容,刷新表格
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.goodsNum > 1)
            {
                model.goodsNum --;
                model.remainedNum++;
            }
           // [self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 12:
        {
            //做加法
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.remainedNum>0) {
                model.remainedNum --;
                model.goodsNum++;
            }
            //[self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 13:
        {    //先獲取到當期行數據源內容,改變數據源內容,刷新表格
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.selectState)            {
                model.selectState = NO;
            }else {
                model.selectState = YES;

            }
            //刷新當前行
            //[self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        default:
            break;
    }
    //刷新表格
    [self.goodsTableView reloadData];
    
    //計算總價
    [self totalPrice];
    
}
#pragma mark -- 計算價格
-(void)totalPrice
{
    //遍歷整個數據源,然後判斷如果是選中的商品,就計算價格(單價 * 商品數量)
    for ( int i =0; i<self.infoArr.count; i++)    {
        GoodsInfoModel *model = [self.infoArr objectAtIndex:i];
        if (model.selectState)
        {
            self.allPrice +=  model.goodsNum ;//[model.goodsPrice intValue];
            self.goodsNum +=model.goodsNum;
        }
    }
    //給總價文本賦值
    self.settlementView.sumLab.text = [NSString stringWithFormat:@"%ld 元",(long)self.allPrice];
    self.settlementView.goodsNumLab.text=[NSString stringWithFormat:@"共計:%ld 件商品 ",(long)self.goodsNum];
    //每次算完要重置為0,因為每次的都是全部迴圈算一遍
    self.allPrice = 0;
    self.goodsNum = 0;
}
//結算
-(void)goStatement{
    NSInteger total=[self.settlementView.sumLab.text integerValue];
    if (total>0) {
        StatementViewController *statementVC=[[StatementViewController alloc]init];
        statementVC.price=total;
        [self.navigationController pushViewController:statementVC animated:YES];
        
    }
}
//全選
-(void)goAllSelect:(UIButton *)sender{
    //判斷是否選中,是改成否,否改成是,改變圖片狀態
    sender.tag = !sender.tag;
    if (sender.tag)    {
        [sender setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
        
    }else{
        [sender setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    }
    //改變單元格選中狀態
    for (int i=0; i<self.infoArr.count; i++)
    {
        GoodsInfoModel *model = [self.infoArr objectAtIndex:i];
        model.selectState = sender.tag;
    }
    //計算價格
    [self totalPrice];
    //刷新表格
    [self.goodsTableView reloadData];

}

 

StatementViewController.h 結算試圖

@property(nonatomic,assign)NSInteger price;//總價

StatementViewController.m

#import "StatementViewController.h"
#import "SC.pch"
@interface StatementViewController ()
@end

@implementation StatementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    UILabel *allPrice=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 160)];
    allPrice.text= [NSString stringWithFormat:@"-----%ld-------",(long)self.price];
    [self.view addSubview:allPrice];
    
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ARC
    ARC是什麼 ARC是iOS 5推出的新功能,全稱叫 ARC(Automatic Reference Counting)。簡單地說,就是代碼中自動加入了retain/release,原先需要手動添加的用來處理記憶體管理的引用計數的代碼可以自動地由編譯器完成了。 該機能在 iOS 5/ Mac OS X ...
  • Android開發中經常需要使用Adapter。 傳統方法是自定義一個Adapter並繼承AndroidSDK內的BaseAdapter, 這種方式代碼量大,耦合度高,靈活性差(各種監聽事件需要對View單獨寫,或者自定義一個比較統一的方法); 而ZBLibrary中的BaseViewAdapter ...
  • “階段一”是指我第一次系統地學習Android開發。這主要是對我的學習過程作個記錄。 在上一篇階段一:解析JSON中提到,最近在寫一個很簡單的天氣預報應用。即使功能很簡單,但我還是想把它做成一個相對完整的應用。這樣的話,像以前想到什麼就做什麼,顯然是不行的,很容易就亂了。所以我就琢磨了一下,弄個什麼 ...
  • Android系統的開機動畫可分為三個部分,kernel啟動,init進程啟動,android系統服務啟動。這三個開機動畫都是在一個叫做 幀緩衝區(frame buffer)的硬體設備上進行渲染繪製的。http://hovertree.com/menu/android/ 在Linux內核中,每一個硬 ...
  • 如果是自己通過repo和git直接從google官網上download的源碼,請忽略這個問題,但是由於google在國內被限制登錄,通過這一種方法不是每個人都能download下來源碼,通常的做法就是從別人那拷貝,然後自己編譯,那麼通常會出現下麵的錯誤: No rule to make target ...
  • 1.統一界面管理 1.1利用一個activity去管理應用的所有的界面 1.1.1 理解Activity,Window和View之間的關係 1.1.2 避免Activity過多導致的問題, 例如:徹底退出應用,頻繁改動清單文件等 統一界面風格,降低用戶的學習成本 2.界面劃分 2.1展示效果圖,將界 ...
  • 當我們在使用tableview時,往往需要在cell左滑時顯示一個或是多個按鈕,但系統預設的只可顯示一個,如常見的刪除按鈕,那麼當我們的需求要求要有多個按鈕時又該怎麼辦呢,我們往下看。 首先,現看看系統的按鈕(只顯示一個按鈕時) //設置cell左滑後的刪除按鈕文字 -(NSString *)tab ...
  • //佈局相關<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" an ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...