您的位置:首页 > 产品设计 > UI/UE

IOS 自定义 UIView 实现重用

2015-11-07 17:23 501 查看

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;
}

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息