• IOS自定义控件


    1 #ifndef GlobalDefine_h
    2 #define GlobalDefine_h
    3 
    4 #define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width
    5 #define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height
    6 
    7 #endif /* GlobalDefine_h */
    1 #import <UIKit/UIKit.h>
    2 
    3 @interface ViewController : UIViewController
    4 
    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 @end
    13 
    14 @implementation ViewController
    15 
    16 - (void)initModel
    17 {
    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)viewDidLoad
    28 {
    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.height
    17         _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)object
    34 {
    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
  • 相关阅读:
    小技巧
    sql日期函数
    c#发送邮件
    js点滴
    Js序列化时间
    js中string的操作
    原系统中有AD FS , CRM Server ,迁移ADFS 到另一台电脑 , CRM Server用443端口出错
    解决UR 12后ISV目录不能用的问题
    Lucene .Net 版本
    Android 开源项目
  • 原文地址:https://www.cnblogs.com/songlei0601/p/5774284.html
Copyright © 2020-2023  润新知