IOS 自定义 UIView 实现重用

IOS 自定义 UIView 实现重用

好的代码应该是简洁、精炼的,这样不仅可以减少包的大小还可以提高内存的使用率和减轻后期维护负担。

如下示例,介绍了 自定义UIView 实现重用,已满足复杂的UI 布局

MarketTopItemView.h
#import <UIKit/UIKit.h>

@interface MarketTopItemView : UIView{
      //code
      NSString *strCode;

      //品种名称
      IBOutlet UILabel *labName;

      //当前价
      IBOutlet UILabel *labCurrentPic;

      //涨跌
      IBOutlet UILabel *labUpDown;

      //涨跌幅
      IBOutlet UILabel *labUpDownRate;

      //背景线
      IBOutlet UIImageView *imgLine;

      //保留小数位
      NSInteger intDecimal;
}


-(id) initViewWithFrame:(CGRect) frame;

-(void) initData:(NSDictionary *) dicData;

@end


 

MarketTopItemView.m

#import "MarketTopItemView.h"

@implementation MarketTopItemView

-(id) initViewWithFrame:(CGRect) frame{

    self = [super initWithFrame:frame];
    if (self) {
        // Custom initialization
    }

    // [S] 初始化子控件
    if(!labName){
        labName = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 6.f, 66.f, 20.f)];
        [labName setFont:[UIFont systemFontOfSize:13.f]];
        [labName setTextAlignment:NSTextAlignmentLeft];
        [labName setTextColor:[UIColor whiteColor]];

        [self addSubview:labName];
    }


    if(!labCurrentPic){
        labCurrentPic = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 26.f, 85.f, 21.f)];
        [labCurrentPic setFont:[UIFont systemFontOfSize:19.f weight:bold]];
        [labCurrentPic setTextAlignment:NSTextAlignmentLeft];
        [labCurrentPic setTextColor:[UIColor whiteColor]];

        [self addSubview:labCurrentPic];
    }


    if(!labUpDown){
        labUpDown = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 45.f, 45.f, 20.f)];
        [labUpDown setFont:[UIFont systemFontOfSize:11.f]];
        [labUpDown setTextAlignment:NSTextAlignmentLeft];
        [labUpDown setTextColor:[UIColor whiteColor]];

        [self addSubview:labUpDown];
    }


    if(!labUpDownRate){
        labUpDownRate = [[UILabel alloc] initWithFrame:CGRectMake(55.f, 45.f, 55.f, 20.f)];
        [labUpDownRate setFont:[UIFont systemFontOfSize:11.f]];
        [labUpDownRate setTextAlignment:NSTextAlignmentLeft];
        [labUpDownRate setTextColor:[UIColor whiteColor]];

        [self addSubview:labUpDownRate];
    }


    if(!imgLine){
        imgLine = [[UIImageView alloc] initWithFrame:CGRectMake(10.f, 65.f, 95.f, 2.f)];

        [self addSubview:imgLine];
    }

    
    [self setBackgroundColor:[UIColor clearColor]];
    // [B] 初始化子控件

    return self;

}

-(void) initData:(NSDictionary *) dicData{

    if (dicData && dicData.count > 0) {
        float upDownRate = 0.f;

        //code
        strCode = dicData[@"Code"];

        //保留位数
        intDecimal = [dicData[@"Decimal"] intValue];

        //名称
        labName.text = dicData[@"Name"];

        //当前价
        if (intDecimal == 2)
            labCurrentPic.text = [NSString stringWithFormat:@"%.2f",[dicData[@"Last"] floatValue]];
        else
            labCurrentPic.text = [NSString stringWithFormat:@"%.4f",[dicData[@"Last"] floatValue]];

        //涨跌
        if (intDecimal == 2)
            labUpDown.text =[NSString stringWithFormat:@"%.2f",[dicData[@"UpDown"] floatValue]];
        else
            labUpDown.text =[NSString stringWithFormat:@"%.4f",[dicData[@"UpDown"] floatValue]];

        //涨跌幅(涨跌 / 昨收 * 100)
        upDownRate = [dicData[@"UpDown"] floatValue] / [dicData[@"LastClose"] floatValue] * 100;
        labUpDownRate.text = [NSString stringWithFormat:@"%.2f",upDownRate];
        labUpDownRate.text = [labUpDownRate.text stringByAppendingString:@"%"];

        //涨跌背景图
        CGRect rect = imgLine.frame;
        float fwidth = abUpDown.frame.size.width;
        fwidth += [AFUtils get_width_for_string:abUpDownRate.text withSize:11.F andWidth:labUpDownRate.frame.size.width];
        rect.size.width = fwidth;

        if ([dicData[@"UpDown"] floatValue] > 0)       //红涨
            imgLine.image = [UIImage imageNamed:@"market_updark_bg.png"];
        else if ([dicData[@"UpDown"] floatValue] < 0)  //绿跌
            imgLine.image = [UIImage imageNamed:@"market_downdark_bg.png"];
        else                                           //白平
            imgLine.image = [UIImage imageNamed:@"market_nomraldark_bg.png"];
        imgLine.frame = rect;

    }

}

@end

调用如下:

if (excodeListArray && [excodeListArray count] > 0) {

        float x = 0.f;
        NSDictionary *dicTemp;
        MarketTopItemView *itemView;
        CGRect rect = CGRectMake(0, 0, 106.5f, 73.f);

        //先清空之前绑定
        for (id sender in [self.topView subviews]) {
            //背景图,不能移除
            if (!([sender isKindOfClass:[UIImageView class]] && ((UIImageView *)sender).tag == 555))
              [sender removeFromSuperview];
        }

        //重新绑定
        for (int i = 0,len = (int)[excodeListArray count]; i < len; i++) {

            //初始化位置
            x = i * 106.5f;
            rect.origin.x = x;
            itemView = [[MarketTopItemView alloc] initViewWithFrame:rect];

            //获取数据
            dicTemp = [excodeListArray objectAtIndex:i];
            [itemView initData:dicTemp];

            [self.topView addSubview:itemView];
        }

        dicTemp = nil;
        itemView = nil;
    }

效果图:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追夢秋陽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值