1

我有一个包含多个产品的 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];
   }

有没有人有办法解决吗?

4

1 回答 1

1

由于 Product *p 是一个指针,因此您不必在数组中删除和重新插入。您可以就地修改产品。尝试这个:

NSInteger index = stepper.tag;

Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];
于 2013-09-11T14:22:59.083 回答