这篇文章主要为大家详细介绍了iOS搭建简易购物车页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了iOS实现简单购物车页面的搭建,供大家参考,具体内容如下
1.基础页面的搭建
- 在storyboard的cell中创建控件并进行约束,继承自定义的AZWineCell
- 将cell中的子控件和自定义的AZWineCell一一进行连线
@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;- 让商品的增加和删减按钮继承于自定义的按钮,实现自定义样式
-(void)awakeFromNib
{
self.layer.borderWidth=1;
self.layer.borderColor=[UIColor orangeColor].CGColor;
self.layer.cornerRadius=self.frame.size.width*0.5;
}2.加载模型数据
- 这里使用懒加载的方式加载数据
-(NSMutableArray *)wineArray
{
if (!_wineArray) {
// 获得路径
NSString *path=[[NSBundle mainBundle]pathForResource:@"wine.plist" ofType:nil];
// 获得数组
NSArray *array=[NSArray arrayWithContentsOfFile:path];
// 创建一个临时数组存放模型数据
NSMutableArray *tempArray=[NSMutableArray array];
// 添加模型
for (NSDictionary *dict in array) {
//字典转模型
AZWine *wine=[AZWine wineWithDict:dict];
// 实现对wine模型内num值变化的监听
[wine addObserver:self forKeyPath:@"num" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
[tempArray addObject:wine];
}
_wineArray=tempArray;
}
return _wineArray;;
}- 给cell绑定模型数据,在模型的set方法给cell注入数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 绑定标识
static NSString *ID=@"wine";
// 创建cell
AZWineCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
// 给cell注入数据
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.设置代理,实现对按钮点击事件的监听
- 自定义协议,提供协议方法供代理调用,监听按钮的点击
@protocol AZWineCellDelegate <NSObject>
@optional
/*增加商品按钮的点击*/
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell;
/*删减商品按钮的点击*/
-(void)wineCellDidClickMinusButton:(AZWineCell *)cell;
@end
@interface AZWineCell : UITableViewCell
/*模型*/
@property(nonatomic,strong)AZWine *wine;
/*设置代理*/
@property(nonatomic, weak) id<AZWineCellDelegate> delegate;
@end- 修改模型数据,修改界面,通知代理实现协议方法
- (IBAction)minusClick {
// 修改模型
self.wine.num--;
// 修改界面
self.countLabel.text=[NSString stringWithFormat:@"%zd",self.wine.num];
// 按钮是否可以点击
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];
// 按钮是否可以点击
self.minusBtn.enabled=YES;
// 通知代理
if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
[self.delegate wineCellDidClickPlusButton:self];
}
}- 实现协议方法,完成总价的刷新
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell
{
// 计算总价
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];
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:iOS搭建简易购物车页面
基础教程推荐
猜你喜欢
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Android中的webview监听每次URL变化实例 2023-01-23
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- android studio按钮监听的5种方法实例详解 2023-01-12
- Flutter手势密码的实现示例(附demo) 2023-04-11
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Android多返回栈技术 2023-04-15
