iOS搭建簡(jiǎn)易購(gòu)物車頁(yè)面
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車頁(yè)面的搭建,供大家參考,具體內(nèi)容如下
1.基礎(chǔ)頁(yè)面的搭建
- 在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];
? ? // 購(gòu)買按鈕
? ? self.purchaseBtn.enabled=YES;
? ? // 購(gòu)物車
? ? if (![self.shoppingList containsObject:cell.wine]) {
? ? ? ? [self.shoppingList addObject:cell.wine];
? ? }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS報(bào)Multiple?commands?produceMultiple錯(cuò)誤的解決方案
這篇文章主要為大家介紹了iOS報(bào)Multiple?commands?produceMultiple錯(cuò)誤的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
iOS 超級(jí)簽名之描述文件的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了iOS 超級(jí)簽名實(shí)現(xiàn)之描述文件的實(shí)現(xiàn)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
IOS 開發(fā)之Object-C中的對(duì)象詳解
這篇文章主要介紹了IOS 開發(fā)之Object-C中的對(duì)象詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
為textView添加語(yǔ)音輸入功能的實(shí)例代碼(集成訊飛語(yǔ)音識(shí)別)
下面小編就為大家分享一篇為textView添加語(yǔ)音輸入功能的實(shí)例代碼(集成訊飛語(yǔ)音識(shí)別),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS學(xué)習(xí)筆記之遠(yuǎn)程推送、靜默推送與自定義消息推送
推送是各位iOS開發(fā)者們都會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于iOS學(xué)習(xí)筆記之遠(yuǎn)程推送、靜默推送與自定義消息推送的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08
淺述iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)
這篇文章主要介紹了 iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)的相關(guān)資料,需要的朋友可以參考下2017-10-10

