ViewController.m中
#import "ViewController.h"
#import "MyTableViewCell.h"
#import "MyViewController.h"
#import "Model.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,ModelDelegate>
{
UITableView *table;
NSArray *path;
NSDictionary *dic;
NSMutableArray *arr;
// UILabel *ll;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
table = [[UITableView alloc]initWithFrame:self.view.frame];
table.delegate = self;
table.dataSource = self;
table.rowHeight = 120;
[self.view addSubview:table];
UILabel *ll = [[UILabel alloc]initWithFrame:CGRectMake(30, self.view.frame.size.height-80, 100, 40)];
ll.text = @"总价位:";
[self.view addSubview:ll];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"wty" ofType:@"plist"];
path = [[NSArray alloc]initWithContentsOfFile:plistPath];
arr=[[NSMutableArray alloc]init];
for (int i=0; i<20; i++) {
Model *mode=[[Model alloc]init];
mode.Numbercount=0;
mode.row = i;
[arr addObject:mode];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return path.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
dic = path[indexPath.row];
cell.img.image = [UIImage imageNamed:[dic objectForKey:@"img"]];
cell.lab1.text = [dic objectForKey:@"name"];
cell.lab2.text = [dic objectForKey:@"jiage"];
cell.delegate = self;
cell.lab3.text=[NSString stringWithFormat:@"%ld",[arr[indexPath.row] Numbercount ]];
Model * MM = arr [indexPath.row];
cell.model = MM;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
dic = path[indexPath.row];
MyViewController *myView = [[MyViewController alloc]init];
myView.img = [dic objectForKey:@"img"];
myView.name = [dic objectForKey:@"name"];
[self.navigationController pushViewController:myView animated:YES];
}
-(void)regiestAddBtn:(MyTableViewCell *)cell Model:(Model *)mm{
mm.Numbercount +=1;
[table reloadData];
}
-(void)regiestCutBtn:(MyTableViewCell *)cell Model:(Model *)mm{
if (mm.Numbercount > 0) {
mm.Numbercount -=1;
}else{
return;
}
[table reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
MyViewController.h中
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@property(nonatomic,strong)NSString *img,*name;
@end
MyViewController.m中
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *vv = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 250, 250)];
vv.image = [UIImage imageNamed:self.img];
[self.view addSubview:vv];
UILabel *ll = [[UILabel alloc]initWithFrame:CGRectMake(150, 400, 100, 100)];
ll.text = self.name;
[self.view addSubview:ll];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
MyTableViewCell.h中
#import <UIKit/UIKit.h>
#import "Model.h"
@class Model;
@class MyTableViewCell;
@protocol ModelDelegate <NSObject>
@required
-(void)regiestAddBtn:(MyTableViewCell *)cell Model:(Model *)mm;
-(void)regiestCutBtn:(MyTableViewCell *)cell Model:(Model *)mm;
@end
@interface MyTableViewCell : UITableViewCell
@property(nonatomic,weak)id<ModelDelegate>delegate;
@property(nonatomic,strong)Model * model;
@property(nonatomic,strong)UIImageView *img;
@property(nonatomic,strong)UILabel *lab1;
@property(nonatomic,strong)UILabel *lab2;
@property(nonatomic,strong)UILabel *lab3;
@property(nonatomic,strong)UIButton *btn1;
@property(nonatomic,strong)UIButton *btn2;
@end
MyTableViewCell.m中
#import "MyTableViewCell.h"
@implementation MyTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.img];
[self.contentView addSubview:self.lab1];
[self.contentView addSubview:self.lab2];
[self.contentView addSubview:self.lab3];
[self.contentView addSubview:self.btn1];
[self.contentView addSubview:self.btn2];
}
return self;
}
-(UIImageView *)img{
if (!_img) {
_img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 70, 70)];
}
return _img;
}
-(UILabel *)lab1{
if (!_lab1) {
_lab1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)];
}
return _lab1;
}
-(UILabel *)lab2{
if (!_lab2) {
_lab2 = [[UILabel alloc]initWithFrame:CGRectMake(150, 60, 200, 30)];
}
return _lab2;
}
-(UILabel *)lab3{
if (!_lab3) {
_lab3 = [[UILabel alloc]initWithFrame:CGRectMake(300, 60, 40, 30)];
_lab3.text = @"0";
}
return _lab3;
}
-(UIButton *)btn1{
if (!_btn1) {
_btn1 = [[UIButton alloc]initWithFrame:CGRectMake(250, 60, 30, 30)];
[_btn1 setTitle:@"-" forState:0];
[_btn1 setTitleColor:[UIColor blackColor] forState:0];
[_btn1 addTarget:self action:@selector(cutclick:) forControlEvents:UIControlEventTouchUpInside];
}
return _btn1;
}
-(UIButton *)btn2{
if (!_btn2) {
_btn2 = [[UIButton alloc]initWithFrame:CGRectMake(330, 60, 30, 30)];
[_btn2 setTitleColor:[UIColor blackColor] forState:0];
[_btn2 setTitle:@"+" forState:0];
[_btn2 addTarget:self action:@selector(addclick:) forControlEvents:UIControlEventTouchUpInside];
}
return _btn2;
}
-(void)addclick:(UIButton *)addBtn{
if ([self.delegate respondsToSelector:@selector(regiestAddBtn:Model:)]) {
[self.delegate regiestAddBtn:self Model:self.model];
NSLog(@"cell=====%p====%ld",self.model,self.model.row);
}
}
-(void)cutclick:(UIButton *)cutBtn{
if ([self.delegate respondsToSelector:@selector(regiestCutBtn:Model:)]) {
[self.delegate regiestCutBtn:self Model:self.model];
}
}
@end
Model.h中
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property(nonatomic,assign)NSInteger Numbercount;
@property(nonatomic,assign)NSInteger row;
@end
网友评论