这篇文章主要为大家详细介绍了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搭建简易购物车页面
猜你喜欢
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Flutter实现底部和顶部导航栏 2022-08-31
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Android实现监听音量的变化 2023-03-30
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Android studio实现动态背景页面 2023-05-23
- Android实现轮询的三种方式 2023-02-17
- iOS 对当前webView进行截屏的方法 2023-03-01
- 详解flutter engine 那些没被释放的东西 2022-12-04
