xcode 代码优化 (2)
自定义控件
新建一个wlisUIView类 并且应用@class wlisZuoye;
#import <UIKit/UIKit.h> @class wlisZuoye; @interface wlisUIView : UIView @property(nonatomic,strong)wlisZuoye * zuoye; @end
再看实现代码
#import "wlisUIView.h"
#import "wlisZuoye.h"
@interface wlisUIView ()
@property(nonatomic,weak)UIImageView * imgImage;
@property(nonatomic,weak)UILabel * labBanner;
@property(nonatomic,weak)UILabel * labContent;
@property(nonatomic,weak)UIButton * btn;
@end
@implementation wlisUIView
-(instancetype)initWithFrame:(CGRect)frame{
if (self=[super initWithFrame:frame]) {
//设置边框宽度
self.layer.borderWidth=1;
//设置边框颜色
self.layer.borderColor=[[UIColor lightGrayColor]CGColor];
UIImageView *imgType=[[UIImageView alloc]init];
//设置容器的边框
imgType.layer.borderWidth=2;
//设置边框的颜色
imgType.layer.borderColor=[[UIColor blueColor]CGColor];
self->_imgImage=imgType;
[self addSubview:self.imgImage];
UILabel * bannerType=[[UILabel alloc]init];
//显示1行数据
bannerType.numberOfLines=1;
//设置居中对齐
bannerType.textAlignment=NSTextAlignmentCenter;
//设置字体大小
bannerType.font=[UIFont systemFontOfSize:16.0];
//设置阴影偏移的位置
bannerType.shadowOffset=CGSizeMake(1.0, 1.0);
//设置边框的宽度
bannerType.layer.borderWidth=1;
//设置边框的颜色
bannerType.layer.borderColor=[[UIColor blackColor]CGColor];
self->_labBanner=bannerType;
[self addSubview:self->_labBanner];
UILabel * contentType=[[UILabel alloc]init];
contentType.numberOfLines=2;
//设置字体大小
contentType.font=[UIFont systemFontOfSize:14.0];
self->_labContent=contentType;
[self addSubview:self->_labContent];
UIButton * btnType=[[UIButton alloc]init];
//设置UIControlStateNormal状态下的文字颜色
[btnType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置字体大小
btnType.titleLabel.font=[UIFont systemFontOfSize:9.0];
//设置边框的宽度
btnType.layer.borderWidth=1;
//设置边框的颜色
btnType.layer.borderColor=[[UIColor lightGrayColor]CGColor];
self->_btn=btnType;
[self addSubview:self->_btn];
//设置父容器的层级关系,显示到最定层
[self bringSubviewToFront:self.btn];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat cgW=self.frame.size.width;
CGFloat cgH=self.frame.size.height;
//设置图片的位置尺寸
self.imgImage.frame=CGRectMake(0, 0, 96, cgH);
//设置标题的位置尺寸
self.labBanner.frame=CGRectMake(self.imgImage.frame.size.width, 0, cgW-self.imgImage.frame.size.width, 30);
//设置内容的位置尺寸
self.labContent.frame=CGRectMake(self.imgImage.frame.size.width, self.labBanner.frame.size.height, cgW-self.imgImage.frame.size.width, cgH-self.labBanner.frame.size.height);
//设置按钮的位置尺寸
self.btn.frame=CGRectMake(cgW-55, cgH-20, 55, 20);
}
-(void)setZuoye:(wlisZuoye *)zuoye{
self->_zuoye=zuoye;
//设置图片
[self->_imgImage setImage:[UIImage imageNamed:zuoye.image]];
//设置标题
[self->_labBanner setText:zuoye.banner];
//设置内容
[self->_labContent setText:zuoye.content];
//设置按钮文字
[self->_btn setTitle:zuoye.followUp forState:UIControlStateNormal];
}
@end
以上自定义控件写好
接下来上优化控制器中的代码:
#import "ViewController.h" #import "wlisZuoye.h" #import "wlisUIView.h" @interface ViewController () /** 图片 标题 内容 跟帖 一个容器 */ @property(nonatomic,strong) UIImageView * img_image; @property(nonatomic,strong) UILabel * labbanner; @property(nonatomic,strong)UILabel * labcontent; @property(nonatomic,strong)UIButton * btn; //@property(nonatomic,strong)UIView * view_1; @property(nonatomic,strong)NSArray * arrRun; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /* 创建run.plist文件 NSArray * arry=@[ @{@"banner":@"随意1",@"content":@"收到货了就和我二姐呢桑德环境禄口机场是两地分居玩儿就能看到发送到;开发商的黑龙江省的客户机",@"image":@"run_1"}, @{@"banner":@"等风2",@"content":@"等风到的日子里,先从不等待开始,所以春风来不来真的没有什么关系",@"image":@"run_2"} ]; BOOL isbool=[arry writeToFile:@"/Users/xmg/Desktop/run.plist" atomically:YES]; if (isbool) { NSLog(@"成功创见"); } */ //获取屏幕宽度 CGFloat cgW=self.view.frame.size.width; //从模型中获取数据 wlisZuoye * wl1=self.arrRun[0]; wlisZuoye * wl2=self.arrRun[1]; /** 第一行数据 */ wlisUIView * view_1=[[wlisUIView alloc]init]; view_1.frame=CGRectMake(0, 20, cgW, 100); //加入到父容器 [self.view addSubview:view_1]; view_1.zuoye=wl1; /** 第二行数据 */ wlisUIView * view_2=[[wlisUIView alloc]init]; view_2.frame=CGRectMake(0, 20+100+2, cgW, 100); [self.view addSubview:view_2]; view_2.zuoye=wl2; } /** 重写arrRun get方法 */ - (NSArray *)arrRun{ //判断数据是否为空 if (self->_arrRun==nil) { //按名称获取数据的路径 NSString * path=[[NSBundle mainBundle]pathForResource:@"run.plist" ofType:nil]; //数据加载到数组中 NSArray * arr=[NSArray arrayWithContentsOfFile:path]; //创建一个可变的数组 NSMutableArray *arrM=[NSMutableArray array]; //遍历数组 转成模型对象加载到数组中 for (NSDictionary * dic in arr) { [arrM addObject:[wlisZuoye wlisZuoyeWithDic:dic]]; } //给数组赋值 self->_arrRun=arrM; } //返回数组 return self->_arrRun; } @end
想想好像uiview我是一个一个创建的 所以接下计划是用九宫格的形式创建

浙公网安备 33010602011771号