在开发过程中测试避免不了用plist文件测试,今天就来说一下具体的应用
首先我们要写一个plist文件,里面是一个数组套字典的形式
我是重写的表格
.h中
@property (nonatomic,strong)UILabel *name,*price;
@property (nonatomic,strong)UILabel *lab1;
@property (nonatomic,strong)UIImageView *img;
.m
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self addSubview:self.name];
[self addSubview:self.price];
[self addSubview:self.img];
}
return self;
}
-(UIImageView*)img
{
if (!_img) {
_img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
}
return _img;
}
-(UILabel*)name
{
if (!_name)
{
_name = [[UILabel alloc]initWithFrame:CGRectMake(20, 90, 200, 30)];
}
return _name;
}
-(UILabel*)price
{
if (!_price)
{
_price = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 200, 30)];
}
return _price;
}
然后是viewcontroller中的代码部分
两个代理
{UITableView *table;
NSMutableArray *marr;
int Q,W,E,T;
UILabel *lab,*lab1,*lab2,*lab3,*lab4;
UIButton *bu1,*bu2,*bu3,*bu4,*bu5,*bu6,*bu7,*bu8;
}
NSString *plistPath=[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
marr=[[NSMutableArray alloc]initWithContentsOfFile:plistPath];
NSLog(@"%@",marr);
self.view.backgroundColor = [UIColor whiteColor];
table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"z"];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"z"];
}
NSDictionary *dic = marr[indexPath.row];
UIImage *im = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dic objectForKey:@"img"]]]]];
NSLog(@"=======%@",[dic objectForKey:@"img"]);
cell.img.image = im;
cell.name.text = [dic objectForKey:@"name"];
cell.price.text = [dic objectForKey:@"price"];
if (indexPath.row == 0) {
bu1 = [[UIButton alloc]initWithFrame:CGRectMake(250, 50, 50, 30)];
[bu1 setTitle:@"-" forState:UIControlStateNormal];
[bu1 addTarget:self action:@selector(buu1:) forControlEvents:UIControlEventTouchUpInside];
bu1.tag = 10;
[bu1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// bu1.tag = 100;
[cell addSubview:bu1];
lab = [[UILabel alloc]initWithFrame:CGRectMake(320, 50, 50, 30)];
lab.text = @"0";
[cell addSubview:lab];
bu2 = [[UIButton alloc]initWithFrame:CGRectMake(350, 50, 50, 30)];
[bu2 setTitle:@"+" forState:UIControlStateNormal];
bu2.tag =12;
[bu2 addTarget:self action:@selector(buu1:) forControlEvents:UIControlEventTouchUpInside];
[bu2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:bu2];
}
else if (indexPath.row == 1)
{
bu3 = [[UIButton alloc]initWithFrame:CGRectMake(250, 50, 50, 30)];
[bu3 setTitle:@"-" forState:UIControlStateNormal];
[bu3 addTarget:self action:@selector(buu3:) forControlEvents:UIControlEventTouchUpInside];
bu3.tag = 13;
[bu3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// bu3.tag = 300;
[cell addSubview:bu3];
lab1 = [[UILabel alloc]initWithFrame:CGRectMake(320, 50, 50, 30)];
lab1.text = @"0";
[cell addSubview:lab1];
bu4= [[UIButton alloc]initWithFrame:CGRectMake(350, 50, 50, 30)];
[bu4 setTitle:@"+" forState:UIControlStateNormal];
bu4.tag = 14;
[bu4 addTarget:self action:@selector(buu3:) forControlEvents:UIControlEventTouchUpInside];
[bu4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:bu4];
}else if (indexPath.row == 2)
{
bu5 = [[UIButton alloc]initWithFrame:CGRectMake(250, 50, 50, 30)];
[bu5 setTitle:@"-" forState:UIControlStateNormal];
[bu5 addTarget:self action:@selector(buu7:) forControlEvents:UIControlEventTouchUpInside];
bu5.tag = 15;
[bu5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// bu3.tag = 300;
[cell addSubview:bu5];
lab2 = [[UILabel alloc]initWithFrame:CGRectMake(320, 50, 50, 30)];
lab2.text = @"0";
[cell addSubview:lab2];
bu6= [[UIButton alloc]initWithFrame:CGRectMake(350, 50, 50, 30)];
[bu6 setTitle:@"+" forState:UIControlStateNormal];
bu6.tag = 16;
[bu6 addTarget:self action:@selector(buu7:) forControlEvents:UIControlEventTouchUpInside];
[bu6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:bu6];
}else if (indexPath.row == 3)
{
bu7 = [[UIButton alloc]initWithFrame:CGRectMake(250, 50, 50, 30)];
[bu7 setTitle:@"-" forState:UIControlStateNormal];
[bu7 addTarget:self action:@selector(buu8:) forControlEvents:UIControlEventTouchUpInside];
bu7.tag = 17;
[bu7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// bu3.tag = 300;
[cell addSubview:bu7];
lab3 = [[UILabel alloc]initWithFrame:CGRectMake(320, 50, 50, 30)];
lab3.text = @"0";
[cell addSubview:lab3];
bu8= [[UIButton alloc]initWithFrame:CGRectMake(350, 50, 50, 30)];
[bu8 setTitle:@"+" forState:UIControlStateNormal];
[bu8 addTarget:self action:@selector(buu8:) forControlEvents:UIControlEventTouchUpInside];
[bu8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
bu8.tag =18;
[cell addSubview:bu8];
}
return cell;
}
-(void)buu1:(UIButton*)sender
{
if (sender.tag == 10) {
Q--;
if (Q>=0) {
lab.text=[NSString stringWithFormat:@"%d",Q];
}
}else if (sender.tag == 12)
{
Q ++;
lab.text=[NSString stringWithFormat:@"%d",Q];
}
}
-(void)buu3:(UIButton*)sender
{
if (sender.tag == 13) {
W--;
if (W>=0) {
lab1.text=[NSString stringWithFormat:@"%d",W];
}
}else if (sender.tag == 14)
{
W ++;
lab1.text=[NSString stringWithFormat:@"%d",W];
}
}
-(void)buu7:(UIButton*)sender
{
if (sender.tag == 15) {
E--;
if (E>=0) {
lab2.text=[NSString stringWithFormat:@"%d",E];
}
}else if (sender.tag == 16)
{
E ++;
lab2.text=[NSString stringWithFormat:@"%d",E];
}
}
-(void)buu8:(UIButton*)sender
{
if (sender.tag == 17) {
T--;
if (T>=0) {
lab3.text=[NSString stringWithFormat:@"%d",T];
}
}else if (sender.tag == 18)
{
T ++;
lab3.text=[NSString stringWithFormat:@"%d",T];
}
}
这就是可以用plist实现简单的点餐界面