大神求助,如何获取对应cell中的label,并改变label的值
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"mycell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 50)];
label.text =[array objectAtIndex:indexPath.row];
//减法按钮
UIButton *leftbtn = [UIButton buttonWithType: UIButtonTypeCustom];
leftbtn.frame = CGRectMake(10, 55, 40, 40);
leftbtn.tag = indexPath.row;
[leftbtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[leftbtn setTitle:@"-" forState:UIControlStateNormal];
leftbtn.backgroundColor = [UIColor greenColor];
//显示改变值的地方
textlabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 55, 30, 40)];
textlabel.text =@"1";
textlabel.textAlignment = NSTextAlignmentCenter;
textlabel.tag = indexPath.row;
//加法按钮
UIButton *rightbtn = [UIButton buttonWithType: UIButtonTypeCustom];
rightbtn.frame = CGRectMake(80, 55, 40, 40);
rightbtn.tag = indexPath.row;
[rightbtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rightbtn setTitle:@"+" forState:UIControlStateNormal];
rightbtn.backgroundColor = [UIColor greenColor];
[cell addSubview:label];
[cell addSubview:leftbtn];
[cell addSubview:textlabel];
[cell addSubview:rightbtn];
return cell;
}
//下面是两个按钮的点击事件
//left是 “-” 按钮
-(void)leftBtnClick:(UIButton *)sender
{
NSLog(@"leftBtnClick:%ld",(long)sender.tag);
--num;
//想要使textlabel 的值 等于num 变化的值。在对应的cell中 下面这个方法是错误的。只能使得最后一个cell 的textlabel 的值改变
textlabel.text = [NSString stringWithFormat:@"%d",num];
}
//right是 “+”号按钮
-(void)rightBtnClick:(UIButton *)sender
{
NSLog(@"rightBtnClick:%ld",(long)sender.tag);
++num;
textlabel.text = [NSString stringWithFormat:@"%d",num];
}
{
static NSString *identifier = @"mycell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 50)];
label.text =[array objectAtIndex:indexPath.row];
//减法按钮
UIButton *leftbtn = [UIButton buttonWithType: UIButtonTypeCustom];
leftbtn.frame = CGRectMake(10, 55, 40, 40);
leftbtn.tag = indexPath.row;
[leftbtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[leftbtn setTitle:@"-" forState:UIControlStateNormal];
leftbtn.backgroundColor = [UIColor greenColor];
//显示改变值的地方
textlabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 55, 30, 40)];
textlabel.text =@"1";
textlabel.textAlignment = NSTextAlignmentCenter;
textlabel.tag = indexPath.row;
//加法按钮
UIButton *rightbtn = [UIButton buttonWithType: UIButtonTypeCustom];
rightbtn.frame = CGRectMake(80, 55, 40, 40);
rightbtn.tag = indexPath.row;
[rightbtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[rightbtn setTitle:@"+" forState:UIControlStateNormal];
rightbtn.backgroundColor = [UIColor greenColor];
[cell addSubview:label];
[cell addSubview:leftbtn];
[cell addSubview:textlabel];
[cell addSubview:rightbtn];
return cell;
}
//下面是两个按钮的点击事件
//left是 “-” 按钮
-(void)leftBtnClick:(UIButton *)sender
{
NSLog(@"leftBtnClick:%ld",(long)sender.tag);
--num;
//想要使textlabel 的值 等于num 变化的值。在对应的cell中 下面这个方法是错误的。只能使得最后一个cell 的textlabel 的值改变
textlabel.text = [NSString stringWithFormat:@"%d",num];
}
//right是 “+”号按钮
-(void)rightBtnClick:(UIButton *)sender
{
NSLog(@"rightBtnClick:%ld",(long)sender.tag);
++num;
textlabel.text = [NSString stringWithFormat:@"%d",num];
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询