zoukankan      html  css  js  c++  java
  • 自定义控件

    步骤

    1. 新建一个继承UIView的类
    2. 重写init方法,在init方法中添加子控件
    3. 在layoutSubviews方法中设置子控件的frame(layoutSubviewsy一定要调用super )
    4. 提供一个模型属性,重写模型属性的set方法(在set方法中取出模型属性,给对应的子控件赋值)
    //来自文件自定义控件
    #import "XMGShopView.h" #import "XMGShopModel.h" @interface XMGShopView () @property (nonatomic ,weak)UIImageView *iconImageView; @property (nonatomic ,weak)UILabel *nameLabel; @end @implementation XMGShopView // 添加子控件 // 控件的init方法内部会自动调用initWithFrame: - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 添加图片 UIImageView *iconImageView = [[UIImageView alloc] init]; [self addSubview:iconImageView]; self.iconImageView = iconImageView; // 添加文字 UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:nameLabel]; self.nameLabel = nameLabel; } return self; } - (instancetype)initWithShop:(XMGShopModel *)shop { if (self = [super init]) { self.shop = shop; } return self; } + (instancetype)shopViewWithShop:(XMGShopModel *)shop { XMGShopView *shopView = [[self alloc] initWithShop:shop]; return shopView; } // 布局子控件,设置子控件的位置和尺寸 - (void)layoutSubviews { // 注意点:这里一定要写 [super layoutSubviews]; CGFloat shopW = self.frame.size.width; CGFloat shopH = self.frame.size.height; self.iconImageView.frame = CGRectMake(0, 0, shopW, shopW); self.nameLabel.frame = CGRectMake(0, shopW, shopW, shopH - shopW); } // 设置子控件显示的数据 - (void)setShop:(XMGShopModel *)shop { _shop = shop; self.iconImageView.image = [UIImage imageNamed:shop.icon]; self.nameLabel.text = shop.name; } #pragma mark - 第二种设置数据的方法实现 - (void)setIcon:(NSString *)icon { self.iconImageView.image = [UIImage imageNamed:icon]; } - (void)setName:(NSString *)name { self.nameLabel.text = name; } - (void)setIcon:(NSString *)icon name:(NSString *)name; { self.iconImageView.image = [UIImage imageNamed:icon]; self.nameLabel.text = name; } @end
  • 相关阅读:
    [原]OpenSSL SSL连接初始化部分解析
    [转]C++日志系统log4cxx使用总结
    js打字效果
    抓取网页Email地址
    jQuery语法总结及注意事项
    Reporting Services中参数说明(因为在框架中要在新的窗口打开报表,所以这理主要是rc:LinkTarget)
    jQuery性能优化指南
    安装文件制作总结
    alert弹出层(待完善……)
    我的tab页面,Jquery方便扩展
  • 原文地址:https://www.cnblogs.com/wwjwb/p/12650112.html
Copyright © 2011-2022 走看看