iOS搭建簡易購物車頁面
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡單購物車頁面的搭建,供大家參考,具體內(nèi)容如下
1.基礎(chǔ)頁面的搭建
- 在storyboard的cell中創(chuàng)建控件并進(jìn)行約束,繼承自定義的AZWineCell
- 將cell中的子控件和自定義的AZWineCell一一進(jìn)行連線
@property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *countLabel; @property (weak, nonatomic) IBOutlet AZWineButton *minusBtn;
- 讓商品的增加和刪減按鈕繼承于自定義的按鈕,實(shí)現(xiàn)自定義樣式
-(void)awakeFromNib { ? ? self.layer.borderWidth=1; ? ? self.layer.borderColor=[UIColor orangeColor].CGColor; ? ? self.layer.cornerRadius=self.frame.size.width*0.5; }
2.加載模型數(shù)據(jù)
- 這里使用懶加載的方式加載數(shù)據(jù)
-(NSMutableArray *)wineArray { ? ? if (!_wineArray) { ? ? ? ? // 獲得路徑 ? ? ? ? NSString *path=[[NSBundle mainBundle]pathForResource:@"wine.plist" ofType:nil]; ? ? ? ? // 獲得數(shù)組 ? ? ? ? NSArray *array=[NSArray arrayWithContentsOfFile:path]; ? ? ? ? // 創(chuàng)建一個(gè)臨時(shí)數(shù)組存放模型數(shù)據(jù) ? ? ? ? NSMutableArray *tempArray=[NSMutableArray array]; ? ? ? ? // 添加模型 ? ? ? ? for (NSDictionary *dict in array) { ? ? ? ? ? ? //字典轉(zhuǎn)模型 ? ? ? ? ? ? AZWine *wine=[AZWine wineWithDict:dict]; ? ? ? ? ? ? // 實(shí)現(xiàn)對(duì)wine模型內(nèi)num值變化的監(jiān)聽 ? ? ? ? ? ? [wine addObserver:self forKeyPath:@"num" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; ? ? ? ? ? ? [tempArray addObject:wine]; ? ? ? ? } ? ? ? ? _wineArray=tempArray; ? ? } ? ? return _wineArray;; }
- 給cell綁定模型數(shù)據(jù),在模型的set方法給cell注入數(shù)據(jù)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ? ? // 綁定標(biāo)識(shí) ? ? static NSString *ID=@"wine"; ? ? // 創(chuàng)建cell ? ? AZWineCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; ? ? // 給cell注入數(shù)據(jù) ? ? cell.wine=self.wineArray[indexPath.row]; ? ? // 返回cell ? ? return cell; }
-(void)setWine:(AZWine *)wine { ? ? _wine=wine; ? ? self.iconView.image=[UIImage imageNamed:wine.image]; ? ? self.nameLabel.text=wine.name; ? ? self.priceLabel.text=wine.money; ? ? self.countLabel.text=[NSString stringWithFormat:@"%zd",wine.num]; ? ? self.minusBtn.enabled=(wine.num>0); }
3.設(shè)置代理,實(shí)現(xiàn)對(duì)按鈕點(diǎn)擊事件的監(jiān)聽
- 自定義協(xié)議,提供協(xié)議方法供代理調(diào)用,監(jiān)聽按鈕的點(diǎn)擊
@protocol AZWineCellDelegate <NSObject> @optional /*增加商品按鈕的點(diǎn)擊*/ -(void)wineCellDidClickPlusButton:(AZWineCell *)cell; /*刪減商品按鈕的點(diǎn)擊*/ -(void)wineCellDidClickMinusButton:(AZWineCell *)cell; @end @interface AZWineCell : UITableViewCell /*模型*/ @property(nonatomic,strong)AZWine *wine; /*設(shè)置代理*/ @property(nonatomic, weak) id<AZWineCellDelegate> delegate; @end
- 修改模型數(shù)據(jù),修改界面,通知代理實(shí)現(xiàn)協(xié)議方法
- (IBAction)minusClick { ? ? // 修改模型 ? ? self.wine.num--; ? ? // 修改界面 ? ? self.countLabel.text=[NSString stringWithFormat:@"%zd",self.wine.num]; ? ? // 按鈕是否可以點(diǎn)擊 ? ? if (self.wine.num==0) { ? ? ? ? self.minusBtn.enabled=NO; ? ? } ? ? // 通知代理 ? ? if([self.delegate respondsToSelector:@selector(wineCellDidClickMinusButton:)]){ ? ? ? ? [self.delegate wineCellDidClickMinusButton:self]; ? ? } } - (IBAction)plusClick { ? ? // 修改模型 ? ? self.wine.num++; ? ? // 修改界面 ? ? self.countLabel.text=[NSString stringWithFormat:@"%zd",self.wine.num]; ? ? // 按鈕是否可以點(diǎn)擊 ? ? self.minusBtn.enabled=YES; ? ? // 通知代理 ? ? if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) { ? ? ? ? [self.delegate wineCellDidClickPlusButton:self]; ? ? } }
- 實(shí)現(xiàn)協(xié)議方法,完成總價(jià)的刷新
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell { ? ? // 計(jì)算總價(jià) ? ? int totalPrice=self.totalPriceView.text.intValue+cell.wine.money.intValue; ? ? // 刷新界面 ? ? self.totalPriceView.text=[NSString stringWithFormat:@"%d",totalPrice]; ? ? // 購買按鈕 ? ? self.purchaseBtn.enabled=YES; ? ? // 購物車 ? ? if (![self.shoppingList containsObject:cell.wine]) { ? ? ? ? [self.shoppingList addObject:cell.wine]; ? ? } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解
這篇文章主要介紹了IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下2017-08-08詳解iOS應(yīng)用UI開發(fā)中的九宮格坐標(biāo)計(jì)算與字典轉(zhuǎn)換模型
這篇文章主要介紹了iOS應(yīng)用UI開發(fā)中的九宮格坐標(biāo)計(jì)算與字典轉(zhuǎn)換模型,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題
今天小編就為大家分享一篇解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08