xcode 代码优化 (1)

这个为效果图
不说了直接上代码 这个是ViewController 控制器的代码
#import "ViewController.h"
#import "wlisZuoye.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 cgX=self.view.frame.origin.x;
// CGFloat cgY=self.view.frame.origin.y;
//获取屏幕宽度
CGFloat cgW=self.view.frame.size.width;
// CGFloat cgH=self.view.frame.size.height;
//从模型中获取数据
wlisZuoye * wl1=self.arrRun[0];
wlisZuoye * wl2=self.arrRun[1];
/**
第一行数据
*/
//创建一个容器UIView 并设置位置
self.view_1=[[UIView alloc]initWithFrame:CGRectMake(0, 20, cgW, 100)];
//self.view_1.backgroundColor=[UIColor lightGrayColor];
//设置边框的宽度
self.view_1.layer.borderWidth=1;
//设置边框的颜色
self.view_1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
//加入到父容器
[self.view addSubview:self.view_1];
//初始化一个装普通的容器 并设置位置
self.img_image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 96, self.view_1.frame.size.height)];
//self.img_image.backgroundColor=[UIColor whiteColor];
//容器中存放图片
[self.img_image setImage: [UIImage imageNamed:wl1.image]];
//设置容器的边框
self.img_image.layer.borderWidth=2;
//设置边框的颜色
self.img_image.layer.borderColor=[[UIColor blueColor]CGColor];
[self.view_1 addSubview:self.img_image];
//创建UILabel 并设置位置
self.labbanner=[[UILabel alloc]initWithFrame:CGRectMake(self.img_image.frame.size.width, 0, cgW-self.img_image.frame.size.width, 30)];
//self.labbanner.backgroundColor=[UIColor redColor];
//显示1行数据
self.labbanner.numberOfLines=1;
//设置居中对齐
self.labbanner.textAlignment=NSTextAlignmentCenter;
//设置字体大小
self.labbanner.font=[UIFont systemFontOfSize:16.0];
//设置阴影偏移的位置
self.labbanner.shadowOffset=CGSizeMake(1.0, 1.0);
//设置文字
[self.labbanner setText:wl1.banner];
//设置边框的宽度
self.labbanner.layer.borderWidth=1;
//设置边框的颜色
self.labbanner.layer.borderColor=[[UIColor blackColor]CGColor];
[self.view_1 addSubview:self.labbanner];
//创建UILabel 并设置位置
self.labcontent=[[UILabel alloc]initWithFrame:CGRectMake(self.img_image.frame.size.width, self.labbanner.frame.size.height, cgW-self.img_image.frame.size.width, self.view_1.frame.size.height-self.labbanner.frame.size.height)];
//self.labcontent.backgroundColor=[UIColor greenColor];
//显示2行数据
self.labcontent.numberOfLines=2;
//设置字体大小
self.labcontent.font=[UIFont systemFontOfSize:14.0];
[self.labcontent setText:wl1.content];
[self.view_1 addSubview:self.labcontent];
//NSLog(@"%@",self.view_1);
//创建一个UIButton 并设置位置
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(self.view_1.bounds.size.width-55, self.view_1.bounds.size.height-20, 55, 20)];
//self.btn.backgroundColor=[UIColor redColor];
//设置UIControlStateNormal状态下的文字颜色
[self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置字体大小
self.btn.titleLabel.font=[UIFont systemFontOfSize:9.0];
//设置边框的宽度
self.btn.layer.borderWidth=1;
//设置边框的颜色
self.btn.layer.borderColor=[[UIColor lightGrayColor]CGColor];
//设置UIControlStateNormal状态下的文字颜色
[self.btn setTitle:wl1.followUp forState:UIControlStateNormal];
//加入父容器中
[self.view_1 addSubview:self.btn];
//设置父容器的层级关系,显示到最定层
[self.view_1 bringSubviewToFront:self.btn];
//NSLog(@"%@",self.btn);
/**
第二行数据
*/
self.view_1=[[UIView alloc]initWithFrame:CGRectMake(0, 20+100+2, cgW, 100)];
self.view_1.layer.borderWidth=1;
self.view_1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
[self.view addSubview:self.view_1];
self.img_image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 96, self.view_1.frame.size.height)];
// self.img_image.backgroundColor=[UIColor whiteColor];
[self.img_image setImage: [UIImage imageNamed:wl2.image]];
self.img_image.layer.borderWidth=2;
self.img_image.layer.borderColor=[[UIColor blueColor]CGColor];
[self.view_1 addSubview:self.img_image];
self.labbanner=[[UILabel alloc]initWithFrame:CGRectMake(self.img_image.frame.size.width, 0, cgW-self.img_image.frame.size.width, 30)];
// self.labbanner.backgroundColor=[UIColor redColor];
self.labbanner.numberOfLines=1;//显示1行
self.labbanner.textAlignment=NSTextAlignmentCenter;//居中对齐
self.labbanner.font=[UIFont systemFontOfSize:16.0];
self.labbanner.shadowOffset=CGSizeMake(1.0, 1.0);
[self.labbanner setText:wl2.banner];
self.labbanner.layer.borderWidth=1;
self.labbanner.layer.borderColor=[[UIColor blackColor]CGColor];
[self.view_1 addSubview:self.labbanner];
self.labcontent=[[UILabel alloc]initWithFrame:CGRectMake(self.img_image.frame.size.width, self.labbanner.frame.size.height, cgW-self.img_image.frame.size.width, self.view_1.frame.size.height-self.labbanner.frame.size.height)];
//self.labcontent.backgroundColor=[UIColor greenColor];
self.labcontent.numberOfLines=2;//显示2行
self.labcontent.font=[UIFont systemFontOfSize:14.0];
[self.labcontent setText:wl2.content];
[self.view_1 addSubview:self.labcontent];
//NSLog(@"%@",self.view_1);
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(self.view_1.bounds.size.width-55, self.view_1.bounds.size.height-20, 55, 20)];
//self.btn.backgroundColor=[UIColor redColor];
[self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.btn.titleLabel.font=[UIFont systemFontOfSize:9.0];
self.btn.layer.borderWidth=1;
self.btn.layer.borderColor=[[UIColor lightGrayColor]CGColor];
[self.btn setTitle:wl2.followUp forState:UIControlStateNormal];
[self.view_1 addSubview:self.btn];
[self.view_1 bringSubviewToFront:self.btn];
}
/**
重写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
在上一个模型的数据
#import <Foundation/Foundation.h> @interface wlisZuoye : NSObject @property(nonatomic,copy)NSString * banner; @property(nonatomic,copy)NSString * content; @property(nonatomic,copy)NSString * image; @property(nonatomic,copy)NSString * followUp; /** 对象构造方法 传入字典 */ -(instancetype)initWithDic:(NSDictionary *)dic; /** 类构造方法 传入字典 */ +(instancetype)wlisZuoyeWithDic:(NSDictionary *)dic; @end
#import "wlisZuoye.h"
@implementation wlisZuoye
-(instancetype)initWithDic:(NSDictionary *)dic{
if (self=[super init]) {
self->_banner=dic[@"banner"];
self->_content=dic[@"content"];
self->_image=dic[@"image"];
self->_followUp=dic[@"followUp"];
}
return self;
}
+(instancetype)wlisZuoyeWithDic:(NSDictionary *)dic{
return [[self alloc]initWithDic:dic];
}
@end
这是第一天的代码 只为了记录自己优化代码的过程
此代码里面数据的获取进行了优化
接下来应该定义一个自定义控件 把控制器的代码在进行优化

浙公网安备 33010602011771号