gpt4 book ai didi

ios - 表中的加号和减号

转载 作者:行者123 更新时间:2023-12-01 18:45:26 25 4
gpt4 key购买 nike

我想要用户添加的项目数量,例如:

-__+

如果用户添加项目,他添加 + 按钮并增加项目。所以我加了 textfield表并添加两个按钮加(+)和减(-)。这是我在 cellForRowAtIndexPath 中的代码:
 if(isPlus){
countFruitQty++;
cell.txtQty.text = [NSString stringWithFormat:@"%i",countFruitQty];
}
else{
countFruitQty--;
if(countFruitQty > 0)
cell.txtQty.text = [NSString stringWithFormat:@"%i",countFruitQty];
else{
cell.txtQty.text = @"";
countFruitQty = 0;
}

但是在滚动时,它将数据更改为所有添加的文本字段。
如何防止这种情况?

最佳答案

你必须为它管理数组,

检查下面的代码供您引用,希望对您有所帮助

@interface YourViewController ()
{
NSMutableArray *arrMain; // Your main array (Table row count)
NSMutableArray *arrCounter; // Counter array
int a;
}

- (void)viewDidLoad {

arrMain = [[NSMutableArray alloc] init];
arrCounter = [[NSMutableArray alloc] init];
a = 0;
for (int i = 0, i < [arrMain count], i++) {

[arrCounter addObject:[NSString stringWithFormat:@"%d",a]];
}
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

cell.btnPlus.tag = indexPath.row;
[cell.btnPlus addTarget:self action:@selector(click_Plus:) forControlEvents:UIControlEventTouchUpInside];

cell.btnMinus.tag = indexPath.row;
[cell.btnMinus addTarget:self action:@selector(click_Minus:) forControlEvents:UIControlEventTouchUpInside];

cell.txtQty.text = [arrCounter objectAtIndex:indexPath.row];
}


-(IBAction)click_Plus:(UIButton *)sender {

int qnt = [cell.txtQty.text intValue];
cell. txtQty.text = [NSString stringWithFormat:@"%d",++qnt];
[arrCounter replaceObjectAtIndex:sender.tag withObject:cell.txtQty.text];
}

-(IBAction)click_Minus:(UIButton *)sender {

int qnt = [cell.txtQty.text intValue];
cell.txtQty.text = [NSString stringWithFormat:@"%d",--qnt];
[arrCounter replaceObjectAtIndex:sender.tag withObject:cell.txtQty.text];
}

关于ios - 表中的加号和减号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36470669/

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