最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)對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)識
? ? 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)對按鈕點(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

时尚| 成安县| 铅山县| 嘉义县| 阿克苏市| 邵武市| 科尔| 绥宁县| 玉林市| 利川市| 封开县| 麻城市| 鄂伦春自治旗| 镇原县| 桂东县| 班戈县| 农安县| 广饶县| 溧水县| 十堰市| 策勒县| 搜索| 绵竹市| 新源县| 本溪市| 石狮市| 万全县| 巴中市| 汶川县| 政和县| 舞阳县| 内黄县| 浦东新区| 古田县| 奉贤区| 阜平县| 淄博市| 怀远县| 平利县| 泰来县| 镇巴县|