我想要由用户添加的项目数,例如:
-__+
如果用户添加项目,则他添加+按钮并增加项目。所以我在表中添加了
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;
}
但是在滚动时,它将数据更改为所有添加的textField。
如何预防呢?
最佳答案
您必须为此管理阵列,
检查下面的代码以供参考,希望对您有所帮助
@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/