gpt4 book ai didi

iphone - 如何处理和检测iPhone中的tableviewcell的多个按钮

转载 作者:行者123 更新时间:2023-11-29 04:53:05 25 4
gpt4 key购买 nike

我想更改 tableviewcell 的加号和减号按钮单击上的项目按钮值。这是我的行索引方法代码的表格 View 单元格。

static NSString *CellIdentifier = @"Cell";

CustomCell *cell = [menuTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
minusBtn.frame= CGRectMake(400, 5,30,30);
minusBtn.tag = indexPath.row;
[minusBtn setImage:[UIImage imageNamed:@"removeButan.png"] forState:UIControlStateNormal];
[minusBtn addTarget:self action:@selector(minusAction:) forControlEvents:UIControlEventTouchUpInside];

UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
plusBtn.frame= CGRectMake(500, 5,30,30);
plusBtn.tag = indexPath.row;
[plusBtn setImage:[UIImage imageNamed:@"Addbutton.png"] forState:UIControlStateNormal];
[plusBtn addTarget:self action:@selector(plusAction:) forControlEvents:UIControlEventTouchUpInside];

UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
itemBtn.frame= CGRectMake(450, 5,30,30);
itemBtn.tag = indexPath.row;
[itemBtn setBackgroundImage:[UIImage imageNamed:@"number_butan_1.png"] forState:UIControlStateNormal];
[itemBtn setTitle:totalItem forState:UIControlStateNormal];
[itemBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

UIButton *addToCart = [UIButton buttonWithType:UIButtonTypeCustom];
addToCart.frame= CGRectMake(580,5,30,30);
addToCart.tag = indexPath.row;
addToCart.font = [UIFont systemFontOfSize:14];
[addToCart setImage:[UIImage imageNamed:@"cart.png"] forState:UIControlStateNormal];
[addToCart addTarget:self action:@selector(addToCartAction:) forControlEvents:UIControlEventTouchUpInside];

UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
infoBtn.frame= CGRectMake(630, 5,30,30);
infoBtn.tag = indexPath.row;
[infoBtn setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
[infoBtn addTarget:self action:@selector(displayDetailView:) forControlEvents:UIControlEventTouchUpInside];

// lblTitle.text = [nameArray objectAtIndex:indexPath.row];
cell.primaryLabel.text = [appDelegate.nameArray objectAtIndex:indexPath.row];
cell.secondaryLabel.text = [appDelegate.sortDetailArray objectAtIndex:indexPath.row];
cell.priceLabel.text = [appDelegate.rateArray objectAtIndex:indexPath.row];

[cell.contentView addSubview:minusBtn];
[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:addToCart];
[cell.contentView addSubview:itemBtn];
[cell.contentView addSubview:infoBtn];

return cell;
}


- (IBAction)plusAction:(id)sender{

// Hat* obj = ((UIButton *)sender).tag;
// NSNumber* hatId = obj.idValue;

UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
UITableView *table=(UITableView*)[cell superview];
NSIndexPath *path=[table indexPathForCell:cell];

int value = [totalItem intValue];
value++;

totalItem = [NSString stringWithFormat:@"%d",value];
NSLog([NSString stringWithFormat:@"%@",totalItem]);
[menuTableView reloadData];

}

- (IBAction)minusAction:(id)sender{

UIView *contentView = [sender superview];

UITableViewCell *cell=(UITableViewCell*)[[contentView superview] superview];
UITableView *table=(UITableView*)[cell superview];
NSIndexPath *path=[table indexPathForCell:cell];

NSUInteger buttonRow = [[menuTableView
indexPathForCell:cell] row];

int value = [totalItem intValue];
value--;

totalItem = [NSString stringWithFormat:@"%d",value];
NSLog([NSString stringWithFormat:@"%@",totalItem]);
[menuTableView reloadData];

}

我正在尝试此操作,但不起作用。它更改了所有行值

最佳答案

您只有一个 TotalItem 实例。如果您有很多行并且想要单独存储每一行​​,则需要创建一个数组来存储每一行​​,然后在 cellForRow 方法中显示适当的值。

关于iphone - 如何处理和检测iPhone中的tableviewcell的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8486696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com