cell
首先,讲解一下本次为大家分享的主要内容 在tablecell中实现点菜功能,具体功能如下
viewcontroll
.h
@property(nonatomic,strong)UILabel *theNameLab;
@property(nonatomic,strong)UILabel *theNumLab;
.m
{
NSMutableArray *data;
int num;//总价格
UITableView *NameTable;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"菜谱";
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Therecipe" ofType:@"plist"];
data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NameTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStyleGrouped];
NameTable.delegate = self;
NameTable.dataSource = self;
NameTable.rowHeight = 120;
NameTable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:NameTable];
_theNameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height-50, 100, 30)];
_theNameLab.text = @"总价格为:";
[self.view addSubview:self.theNameLab];
_theNumLab = [[UILabel alloc]initWithFrame:CGRectMake(110, self.view.frame.size.height-50, 100, 30)];
num = 0;
NSString *stq = [[NSString alloc]initWithFormat:@"%d",num];
_theNumLab.text = stq;
[self.view addSubview:self.theNumLab];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return data.count;
}
//表格cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
num = 0;
static NSString *Str = @"Cell";
NameTableViewCell *theTVC = [tableView dequeueReusableCellWithIdentifier:Str];
if (!theTVC) {
theTVC = [[NameTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
theTVC.ImageView.image = [UIImage imageNamed:[data[indexPath.row]objectForKey:@"ImageView"]];
theTVC.NameLab.text =[data[indexPath.row]objectForKey:@"Name"];
theTVC.PriceLab.text = [data[indexPath.row]objectForKey:@"price"];
theTVC.NumLab.text =[data[indexPath.row]objectForKey:@"Num"];
theTVC.ReductionBut.tag = indexPath.row;
[theTVC.ReductionBut addTarget:self action:@selector(RedyctClick:) forControlEvents:UIControlEventTouchUpInside];
theTVC.addBut.tag = indexPath.row;
[theTVC.addBut addTarget:self action:@selector(AddClick:) forControlEvents:UIControlEventTouchUpInside];
NSString *theS = [[data objectAtIndex:indexPath.row]objectForKey:@"price"];
int jiage = [theS intValue];
NSString *theSQ =[[data objectAtIndex:indexPath.row]objectForKey:@"Num"];
int Geshu = [theSQ intValue];
num = num +jiage*Geshu;
NSLog(@"%d",num);
_theNumLab.text = [NSString stringWithFormat:@"%d",num];
return theTVC;
}
//减号
-(void)RedyctClick:(UIButton *)sender
{
//修改字典里面的内容,先按照结构取到你想修改内容的小字典
NSMutableDictionary *dd = [data objectAtIndex:sender.tag];
NSString *dataStr = [[data objectAtIndex:sender.tag]objectForKey:@"Num"];
int numo = [dataStr intValue];
if (numo>0) {
numo = numo-1;
NSString *numStr = [NSString stringWithFormat:@"%d",numo];
[dd setObject:numStr forKey:@"Num"];
[data removeObjectAtIndex:sender.tag];
[data addObject:dd];
dispatch_async(dispatch_get_main_queue(), ^{
[NameTable reloadData];
});
}
}
//加号
-(void)AddClick:(UIButton *)sender
{
//修改字典里面的内容,先按照结构取到你想修改内容的小字典
NSMutableDictionary *dd = [data objectAtIndex:sender.tag];
NSString *dataStr = [[data objectAtIndex:sender.tag]objectForKey:@"Num"];
int numo = [dataStr intValue];
numo = [dataStr intValue];
numo = numo+1;
NSString *numStr = [NSString stringWithFormat:@"%d",numo];
[dd setObject:numStr forKey:@"Num"];
[data removeObjectAtIndex:sender.tag];
[data addObject:dd];
dispatch_async(dispatch_get_main_queue(), ^{
[NameTable reloadData];
});
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TwoViewController *theTwo = [[TwoViewController alloc]init];
theTwo.theImageStr = [NSString stringWithFormat:@"%@",[[data objectAtIndex:indexPath.row]objectForKey:@"ImageView"]];
theTwo.theLabStr =[NSString stringWithFormat:@"%@",[[data objectAtIndex:indexPath.row]objectForKey:@"Name"]];
[self.navigationController pushViewController:theTwo animated:YES];
}
TwoViewControll
.h定义属性
@property(nonatomic,strong)UIImageView *theImage;
@property(nonatomic,strong)UILabel *theLab;
@property(nonatomic,strong)NSString *theImageStr;
@property(nonatomic,strong)NSString *theLabStr;
.m实现
self.view.backgroundColor = [UIColor whiteColor];
_theImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, self.view.frame.size.width-60,self.view.frame.size.width-60 )];
[self.view addSubview:self.theImage];
_theImage.image = [UIImage imageNamed:_theImageStr];
_theLab = [[UILabel alloc]initWithFrame:CGRectMake(30, self.view.frame.size.width+60, self.view.frame.size.width-60,30 )];
_theLab.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.theLab];
_theLab.text = _theLabStr;
_theLab.textAlignment = NSTextAlignmentCenter;
NameTableView
cell.h
@property(nonatomic,strong)UIImageView *ImageView;
@property(nonatomic,strong)UILabel *NameLab;
@property(nonatomic,strong)UILabel *PriceLab;
@property(nonatomic,strong)UIButton *ReductionBut;
@property(nonatomic,strong)UIButton *addBut;
@property(nonatomic,strong)UILabel *NumLab;
cell.m
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle: style reuseIdentifier:reuseIdentifier];
if (self) {
_ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];
_NameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 95, 80, 20)];
_PriceLab = [[UILabel alloc]initWithFrame:CGRectMake(110, 40, 60, 40)];
[self addSubview:self.ImageView];
[self addSubview:self.NameLab];
[self addSubview:self.PriceLab];
_ReductionBut = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-100, 45, 30, 30)];
[_ReductionBut setTitle:@"-" forState:UIControlStateNormal];
[_ReductionBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self addSubview:self.ReductionBut];
_NumLab = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-70, 45, 40, 30)];
_NumLab.textAlignment = NSTextAlignmentCenter;
_NumLab.backgroundColor = [UIColor yellowColor];
[self addSubview:self.NumLab];
_addBut = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-30, 45, 30, 30)];
[_addBut setTitle:@"+" forState:UIControlStateNormal];
[_addBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self addSubview:self.addBut];
}
return self;
}
其中的plist文件:
以上就是本cell其中的内容,希望对大家有所帮助,感谢朋友们的支持