本文实例为大家分享了iOS实现简单购物车页面的搭建,供大家参考,具体内容如下 1.基础页面的搭建 在storyboard的cell中创建控件并进行约束,继承自定义的AZWineCel
本文实例为大家分享了iOS实现简单购物车页面的搭建,供大家参考,具体内容如下
1.基础页面的搭建
@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;;
}
-(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];
}
}
--结束END--
本文标题: iOS搭建简易购物车页面
本文链接: https://www.lsjlt.com/news/172056.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0