欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS搭建簡易購物車頁面

 更新時(shí)間:2022年08月08日 10:34:17   作者:azhang_coder  
這篇文章主要為大家詳細(xì)介紹了iOS搭建簡易購物車頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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的Cookie存取

    一篇文章搞定iOS的Cookie存取

    Cookie中文名稱叫做“小型文本文件”,指某些網(wǎng)站為了辨別用戶身份而存儲(chǔ)在用戶本地終端上的數(shù)據(jù)(通常經(jīng)過加密),下面這篇文章主要給大家介紹了關(guān)于iOS的Cookie存取的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • 兩行IOS代碼實(shí)現(xiàn)輪播圖

    兩行IOS代碼實(shí)現(xiàn)輪播圖

    這篇文章主要為大家詳細(xì)介紹了兩行IOS代碼實(shí)現(xiàn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解

    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)換模型

    這篇文章主要介紹了iOS應(yīng)用UI開發(fā)中的九宮格坐標(biāo)計(jì)算與字典轉(zhuǎn)換模型,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-01-01
  • Objective-C中NSLog輸出格式大全

    Objective-C中NSLog輸出格式大全

    這篇文章主要介紹了Objective-C中NSLog輸出格式的相關(guān)資料,非常的簡單,有需要的小伙伴可以參考下。
    2015-06-06
  • IOS setOnclick點(diǎn)擊事件分析

    IOS setOnclick點(diǎn)擊事件分析

    本篇文章給大家整理了IOS setOnclick點(diǎn)擊事件完美擴(kuò)展的相關(guān)知識(shí)點(diǎn)以及代碼實(shí)例,有需要的朋友可以跟著測(cè)試學(xué)習(xí)下。
    2018-05-05
  • iPhoneX 各種適配記錄筆記(超全面)

    iPhoneX 各種適配記錄筆記(超全面)

    iPhone X出來之后,關(guān)于劉海的各種適配成了程序員們首要考慮的問題,下面這篇文章主要給大家介紹了關(guān)于iPhoneX 各種適配的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • 解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題

    解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題

    今天小編就為大家分享一篇解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • iOS 清除xcode緩存和生成文件的方法

    iOS 清除xcode緩存和生成文件的方法

    下面小編就為大家分享一篇iOS 清除xcode緩存和生成文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS組件封裝與自動(dòng)布局自定義表情鍵盤

    iOS組件封裝與自動(dòng)布局自定義表情鍵盤

    這篇文章主要介紹了iOS組件封裝與自動(dòng)布局自定義表情鍵盤的相關(guān)資料,需要的朋友可以參考下
    2016-04-04

最新評(píng)論