IOS自訂控制項

來源:互聯網
上載者:User

標籤:

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自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.