我有一个包含多个产品的NSMutableArray。每种产品都有一定数量。我想在单击步进器时更新相关产品的数量。 但我的所有产品(整个NSMutableArray)都使用步进器的数量进行更新。
NSInteger index = stepper.tag;
Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%d", stepper.tag] message:@"test" delegate:nil cancelButtonTitle:@"ok"otherButtonTitles:nil];
// [alert show];
for (Product *p in products) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@", p.amount] message:p.name delegate:nil cancelButtonTitle:@"ok"otherButtonTitles:nil];
[alert show];
}
有没有人有解决方案?
答案 0 :(得分:1)
由于Product * p是一个指针,因此您不必删除并重新插入数组。您只需修改产品即可。试试这个:
NSInteger index = stepper.tag;
Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];