用OC和UI實現購物車功能 在iOS平臺上
購物車專案
作用:可以通過按鈕把物品簡單的新增到購物車中
思路:
一,設定兩個加減按鈕,有普通,高亮和enable狀態,在interface中有屬性宣告
二,設定購物車的imageview,在interface中有屬性宣告
三,設定陣列裝載字典物件,字典裡包含物品圖片的名字和物品標題,寫入一個plist檔案中,然後放到singel view application的support file中
四,建立兩個類,分別是Shop(繼承於NSObject)和ShopView(繼承於UIView)。Shop用於從陣列獲取圖片的名字和標題;ShopView用於設定圖片的frame和標題。
五,設定九宮格,用新增的圖片個數作為索引,計算出每個圖片的距離以及需要換行的數量.
大致如圖
-----------------------------------LHJViewController.m中程式碼實現------------------------------------
//
// LHJViewController.m
// 購物車
//
// Created by 樑華建 on 2017/9/19.
// Copyright © 2017年 樑華建. All rights reserved.
//
#import "LHJShop.h"
#import "LHJViewController.h"
@interface LHJViewController ()
//按鈕的物件
@property (weak, nonatomic) IBOutlet UIButton *Subtrack;
@property (weak, nonatomic) IBOutlet UIButton *AddButton;
//購物車的image
@property (weak, nonatomic) IBOutlet UIImageView *UICarImage;
@property(nonatomic,strong)NSArray *Arrdic;
@end
@implementation LHJViewController
/********************載入資料進入陣列**********************/
-(NSArray *)Arrdic
{
if (_Arrdic==nil) {
//從plist檔案中讀取字典
NSString *PathForPlist=[[NSBundle mainBundle]pathForResource:@"Arr.plist" ofType:nil];
self.Arrdic=[NSArray arrayWithContentsOfFile:PathForPlist];
//建立可變陣列裝載Arrdic
NSMutableArray *dict=[[NSMutableArray alloc]init];
//遍歷
for (NSDictionary *dic in self.Arrdic) {
LHJShop *shop1=[[LHJShop alloc]initWithDictionary:dic];
[dict addObject:shop1];
}
//把可變陣列裝進去
self.Arrdic=dict;
}
return _Arrdic;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
//加入購物車按鈕
- (IBAction)AddToCar:(UIButton *)sender {
//設定圖片數量
NSInteger num=self.UICarImage.subviews.count;
/******************設定九宮格******************/
//列數
NSInteger list=3;
//圖片的xy值
CGFloat xwidht=80;
CGFloat yheight=100;
//計算水平間距和垂直間距
CGFloat xw=(self.UICarImage.frame.size.width-list*xwidht)/3;
CGFloat yh=(self.UICarImage.frame.size.height-2*yheight)/1;
//求出x和y的值
CGFloat x=(xwidht+xw)*(num%list);
CGFloat y=(yheight+yh)*(num/list);
/**************新增購物車圖****************/
// 取出資料
LHJShop *Shop=self.Arrdic[num];
LHJShopView *LHJshopview=[[LHJShopView alloc]initWithShop:Shop];
LHJshopview.frame=CGRectMake(x,y, xwidht, yheight);
[self.UICarImage addSubview:LHJshopview];
/*-----------------讀取資料------------------*/
//設定按鈕狀態
self.AddButton.enabled=(num!=5);
self.Subtrack.enabled=YES;
}
//去除購物車按鈕
- (IBAction)SubtractToCar:(UIButton *)sender{
UIView *lastobject= [self.UICarImage.subviews lastObject];
[lastobject removeFromSuperview];
self.AddButton.enabled=YES;
self.Subtrack.enabled=(self.UICarImage.subviews.count!=0);
}
@end
----------------------------shop中程式碼(獲取字典中的資料)--------------------------
// LHJShop.h
#import <Foundation/Foundation.h>
@interface LHJShop : NSObject
/** 圖片的名稱*/
@property(nonatomic,copy)NSString *name;
/** 商品的名稱*/
@property(nonatomic, copy)NSString *icon;
-(instancetype)initWithDictionary:(NSDictionary *)dic;
+(instancetype)ArrayWithDictionary:(NSDictionary *)dic;
@end
// LHJShop.m
#import "LHJShop.h"
@implementation LHJShop
//設定類工廠和initwith函式 獲得並封裝字典中的圖片名字和圖片標題
-(instancetype)initWithDictionary:(NSDictionary *)dic
{
LHJShop *shop=[[LHJShop alloc ]init];
shop.name=dic[@"name"];
shop.icon=dic[@"icon"];
return shop;
}
+(instancetype)ArrayWithDictionary:(NSDictionary *)dic
{
return [[LHJShop alloc]initWithDictionary:dic];
}
@end
-------------------------ShopView(載入圖片)------------------------------
// LHJShopView.h
@class LHJShop;
#import <UIKit/UIKit.h>
//自定義控制元件
//在init初始化子控制元件
//在layoutSubviews中設定frame
//在外面設定資料
@interface LHJShopView : UIView
@property(nonatomic,strong)LHJShop *Shop;
-(instancetype)initWithShop:(LHJShop *)shop;
+(instancetype)LHJShopViewWithShop:(LHJShop *)shop;
@end
// LHJShopView.m
#import "LHJShopView.h"
#import "LHJShop.h"
@interface LHJShopView ()
//設定圖片
@property(nonatomic,weak)UIImageView *imageview;
//設定文字
@property(nonatomic,weak)UILabel *label;
@end
@implementation LHJShopView
-(instancetype)init
{
//設定圖片
if(self=[super init])
{
[self SetUp];
}
return self;
}
//定義佈局 (設定frame)
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat xwidht=self.frame.size.width;
CGFloat yheight=self.frame.size.height;
self.imageview.frame=CGRectMake(0,0,xwidht,xwidht);
self.label.frame=CGRectMake(0,xwidht,xwidht,yheight-xwidht);
// NSLog(@"%f------%f",xwidht,yheight);
}
+(instancetype)LHJShopViewWithShop:(LHJShop *)shop
{
return [[self alloc]initWithShop:shop];
}
-(instancetype)initWithShop:(LHJShop *)shop
{
if (self=[super init]) {
[self SetUp];
self.Shop=shop;
}
return self;
}
-(void)setShop:(LHJShop *)Shop
{
_Shop=Shop;
self.imageview.image=[UIImage imageNamed:Shop.name];
self.label.text=Shop.icon;
}
-(void)SetUp
{
//設定imageview
UIImageView *imageview=[[UIImageView alloc]init];
imageview.backgroundColor=[UIColor yellowColor];
[self addSubview:imageview];
_imageview=imageview;
//設定label
UILabel *label=[[UILabel alloc]init];
label.textAlignment=NSTextAlignmentCenter;
label.backgroundColor=[UIColor greenColor];
[self addSubview:label];
_label=label;
}
@end
------------------------------------------------------------------- 總結: 難點:自定義控制元件 為了封裝好資料,在內部載入資料,要清楚的知道傳了什麼資料去什麼地方,還有getter或者setter方法的重寫不要寫成死迴圈,整理好思路。 實在找不出問題就打斷點,在每一個傳參後面用NSLog函式列印一下資訊,判斷哪裡資料轉換失敗,本人就因為寫錯一個setter方法搞了了比較長的時間。