標籤:
1 #ifndef GlobalDefine_h2 #define GlobalDefine_h3 4 #define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width5 #define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height6 7 #endif /* GlobalDefine_h */
1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 5 6 @end
1 #import "ViewController.h" 2 #import "IntegratedController.h" 3 #import "GlobalDefine.h" 4 #import "IntegratedModel.h" 5 6 @interface ViewController () 7 8 { 9 NSMutableArray *array;10 }11 12 @end13 14 @implementation ViewController15 16 - (void)initModel17 {18 IntegratedModel *model = [[IntegratedModel alloc] initWithImgName:@"1.png" andTitleName:@"雪花啤酒大特賣" andPrice:1.5];19 IntegratedModel *model1 = [[IntegratedModel alloc] initWithImgName:@"2.png" andTitleName:@"風扇大特賣" andPrice:20.9];20 IntegratedModel *model2 = [[IntegratedModel alloc] initWithImgName:@"3.png" andTitleName:@"籃球特賣" andPrice:23.2];21 IntegratedModel *model3 = [[IntegratedModel alloc] initWithImgName:@"4.png" andTitleName:@"水杯大特賣" andPrice:15.4];22 IntegratedModel *model4 = [[IntegratedModel alloc] initWithImgName:@"5.png" andTitleName:@"插座大特賣" andPrice:15.9];23 24 array = [[NSMutableArray alloc] initWithObjects:model, model1, model2, model3, model4, nil];25 }26 27 - (void)viewDidLoad28 {29 [super viewDidLoad];30 self.view.backgroundColor = [UIColor yellowColor];31 32 [self initModel];33 34 for (int i = 0; i < 5; ++i)35 {36 IntegratedController *intView = [[IntegratedController alloc] initWithFrame:CGRectMake(i % 2 * SCREENWIDTH / 2, i / 2 * SCREENHEIGHT / 3 + 20, SCREENWIDTH / 2 - 2, SCREENHEIGHT / 3)];37 [intView setValue:array[i]];38 [self.view addSubview:intView];39 }40 41 }42 43 - (void)didReceiveMemoryWarning {44 [super didReceiveMemoryWarning];45 // Dispose of any resources that can be recreated.46 }47 48 @end
1 #import <UIKit/UIKit.h> 2 3 //自訂基礎控制項 4 @interface IntegratedController : UIView 5 6 @property (nonatomic, strong) UIImageView *imgView; 7 @property (nonatomic, strong) UILabel *introLabel; 8 @property (nonatomic, strong) UILabel *priceLabel; 9 10 - (void)setValue:(id)object;11 12 @end
1 #import "IntegratedController.h" 2 #import "IntegratedModel.h" 3 4 @implementation IntegratedController 5 6 //(0, 0, screenWidth / 2 - 2, screenHeight / 3) 7 8 - (id)initWithFrame:(CGRect)frame 9 {10 if (self = [super initWithFrame:frame])11 {12 _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height * 2 / 3)];13 [self addSubview:_imgView];14 15 _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _imgView.frame.size.height, frame.size.width, 20)];16 //CGRectGetMaxY(_imgView.frame) = _imgView.frame.size.height17 _introLabel.textAlignment = NSTextAlignmentCenter;18 _introLabel.font = [UIFont systemFontOfSize:16.0];19 _introLabel.textColor = [UIColor blackColor];20 [self addSubview:_introLabel];21 22 _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _imgView.frame.size.height + _introLabel.frame.size.height, frame.size.width, 20)];//_imgView.frame.size.height + _introLabel.frame.size.height = CGRectGetMaxY(_introLabel.frame)23 _priceLabel.textAlignment = NSTextAlignmentCenter;24 _priceLabel.textColor = [UIColor redColor];25 _priceLabel.font = [UIFont boldSystemFontOfSize:16.0];26 _priceLabel.textColor = [UIColor redColor];27 [self addSubview:_priceLabel];28 }29 30 return self;31 }32 33 - (void)setValue:(id)object34 {35 IntegratedModel *obj = (IntegratedModel *)object;36 _imgView.image = [UIImage imageNamed:obj.imgName];37 _introLabel.text = obj.titleName;38 _priceLabel.text = [NSString stringWithFormat:@"%.1f元", obj.price];39 }40 41 @end
1 #import <Foundation/Foundation.h> 2 3 @interface IntegratedModel : NSObject 4 5 @property (nonatomic, strong) NSString *imgName; 6 @property (nonatomic, strong) NSString *titleName; 7 @property (nonatomic, assign) double price; 8 9 - (id)initWithImgName:(NSString *)iName andTitleName:(NSString *)tName andPrice:(double)p;10 11 @end
#import "IntegratedModel.h"@implementation IntegratedModel- (id)initWithImgName:(NSString *)iName andTitleName:(NSString *)tName andPrice:(double)p{ if (self = [super init]) { self.imgName = iName; self.titleName = tName; _price = p; } return self;}@end
IOS自訂控制項