標籤:object action table resource car nil uiview log turn
k控制器:
//// XMGViewController.h#import <UIKit/UIKit.h>@interface XMGViewController : UIViewController@end
//// XMGViewController.m#import "XMGViewController.h"#import "XMGShop.h"#import "XMGShopView.h"@interface XMGViewController ()// 購物車@property (weak, nonatomic) IBOutlet UIView *shopCarView;// 添加按鈕@property (weak, nonatomic) IBOutlet UIButton *addButton;// 刪除按鈕@property (weak, nonatomic) IBOutlet UIButton *removeButton;/** 資料數組 */@property (nonatomic, strong) NSArray *dataArr;@end@implementation XMGViewController/** * 懶載入 */- (NSArray *)dataArr{ if (_dataArr == nil) { // 載入資料 // 1.擷取全路徑 NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil]; self.dataArr = [NSArray arrayWithContentsOfFile:dataPath]; // 字典轉模型 // 建立臨時數組 NSMutableArray *tempArray = [NSMutableArray array]; for (NSDictionary *dict in self.dataArr) { // 建立shop對象 XMGShop *shop = [XMGShop shopWithDict:dict]; // 把模型裝入數組 [tempArray addObject:shop]; } self.dataArr = tempArray; } return _dataArr;}// 初始化資料- (void)viewDidLoad { [super viewDidLoad];}/** * 添加到購物車 * * @param button 按鈕 */- (IBAction)add:(UIButton *)button {/***********************1.定義一些常量*****************************/ // 1.總列數 NSInteger allCols = 3; // 2.商品的寬度 和 高度 CGFloat width = 80; CGFloat height = 100; // 3.求出水平間距 和 垂直間距 CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -1); CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height) / 1; // 4. 設定索引 NSInteger index = self.shopCarView.subviews.count; // 5.求出x值 CGFloat x = (hMargin + width) * (index % allCols); CGFloat y = (vMargin + height) * (index / allCols); /***********************2.建立一個商品*****************************/ /* 原來的分開建立: // 1.建立商品的view UIView *shopView = [[UIView alloc] init]; // 2.設定frame shopView.frame = CGRectMake(x, y, width, height); // 3.設定背景顏色 shopView.backgroundColor = [UIColor greenColor]; // 4.添加到購物車 [self.shopCarView addSubview:shopView]; // 5.建立商品的UIImageView對象 UIImageView *iconView = [[UIImageView alloc] init]; iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor blueColor]; [shopView addSubview:iconView]; // 6.建立商品標題對象 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; // 置中 [shopView addSubview:titleLabel]; */ XMGShopView *shopView = [[XMGShopView alloc] init]; shopView.frame = CGRectMake(x, y, width, height); [self.shopCarView addSubview:shopView]; /***********************3.設定資料*****************************/ // 設定資料 XMGShop *shop = self.dataArr[index]; shopView.iconView.image = [UIImage imageNamed:shop.icon]; shopView.titleLabel.text = shop.name; /***********************4.設定按鈕的狀態*****************************/ button.enabled = (index != 5); // 5.設定刪除按鈕的狀態 self.removeButton.enabled = YES; }/** * 從購物車中刪除 * * @param button 按鈕 */- (IBAction)remove:(UIButton *)button { // 1. 刪除最後一個商品 UIView *lastShopView = [self.shopCarView.subviews lastObject]; [lastShopView removeFromSuperview]; // 3. 設定添加按鈕的狀態 self.addButton.enabled = YES; // 4. 設定刪除按鈕的狀態 self.removeButton.enabled = (self.shopCarView.subviews.count != 0); }@end
自訂控制項;
//// XMGShopView.h#import <UIKit/UIKit.h>@interface XMGShopView : UIView/** 圖片控制項 */@property (nonatomic, weak) UIImageView *iconView;// [self addSubview:iconView];已經有強指標引用了,這裡用weak/** 標題控制項 */@property (nonatomic, weak) UILabel *titleLabel;@end
//// XMGShopView.m#import "XMGShopView.h"@interface XMGShopView ()@end@implementation XMGShopView/** * 初始化子控制項(不要設定frame,擷取的寬度高度是0,在layoutSubviews可以拿到) * */- (instancetype)init{ if (self = [super init]) { /* // 0.擷取當前控制項的尺寸 CGFloat width = self.frame.size.width; CGFloat height = self.frame.size.height; // NSLog(@"init:%f----%f", width, height); */ // 1.建立商品的UIImageView對象 UIImageView *iconView = [[UIImageView alloc] init];// iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor blueColor]; [self addSubview:iconView]; self.iconView = iconView; // 2.建立商品標題對象 UILabel *titleLabel = [[UILabel alloc] init];// titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; // 置中 [self addSubview:titleLabel];//強指標引用了 self.titleLabel = titleLabel;//titleLabel變成全域的,不然layoutSubviews()方法裡面擷取不到, } return self;}/** * 布局子控制項(可以拿到frame) */- (void)layoutSubviews{ // 0.一定要調用super,保留父類系統的布局。否則父類的布局就沒了。 [super layoutSubviews]; // 1.擷取當前控制項的尺寸 CGFloat width = self.frame.size.width;//這個frame是從外面shopView.frame = CGRectMake(x, y, width, height);擷取的 CGFloat height = self.frame.size.height; // 2.設定子控制項的frame self.iconView.frame = CGRectMake(0, 0, width, width); self.titleLabel.frame = CGRectMake(0, width, width, height - width);}@end
bean:
//// XMGShop.h#import <Foundation/Foundation.h>@interface XMGShop : NSObject/** 圖片的名稱 */@property (nonatomic, copy) NSString *icon;/** 商品的名稱 */@property (nonatomic, copy) NSString *name;// 提供構造方法/*- (instancetype)initWithIcon: (NSString *)icon name: (NSString *)name;+ (instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name; */- (instancetype)initWithDict:(NSDictionary *)dict;+ (instancetype)shopWithDict:(NSDictionary *)dict;@end
//// XMGShop.m#import "XMGShop.h"@implementation XMGShop/*- (instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{ if (self = [super init]) { self.icon = icon; self.name = name; } return self;}+ (instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{ return [[self alloc] initWithIcon:icon name:name];} */- (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { self.icon = dict[@"icon"]; self.name = dict[@"name"]; } return self;}+ (instancetype)shopWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict];}@end
ios16--自訂控制項1