Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 索引处的对象更新整个nsmutablearray_Iphone_Ios_Xcode_Nsmutablearray_Updates - Fatal编程技术网

Iphone 索引处的对象更新整个nsmutablearray

Iphone 索引处的对象更新整个nsmutablearray,iphone,ios,xcode,nsmutablearray,updates,Iphone,Ios,Xcode,Nsmutablearray,Updates,我有一个包含多个产品的NSMutableArray。每种产品都有一个数量。我想在单击步进器时更新关联产品的数量。 但我的所有产品(整个NSMutableArray)都会随着步进器的数量而更新 NSInteger index = stepper.tag; Product *p = [products objectAtIndex:index]; p.amount = [NSNumber numberWithDouble:stepper.value]; // UI

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

有人有解决方案吗?

因为Product*p是指针,所以不必删除并重新插入数组。您可以就地修改产品。试试这个:

NSInteger index = stepper.tag;

Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];

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

NSInteger index = stepper.tag;

Product *p = [products objectAtIndex:index];
p.amount = [NSNumber numberWithDouble:stepper.value];

谢谢,但结果还是一样的:sI编辑了我的问题,点击stepper时触发了整个代码。。(当我取消对其中一部分的注释时,我看到了正确的stepper.tag)您确定整个阵列都更新了吗。使用alertView调试代码不是更好的方法,因为show方法在每个元素上都没有停止,而是堆叠它。将alertView替换为NSLog并显示结果。谢谢,但结果仍然相同:sI编辑了我的问题,并在单击stepper时触发了整个代码。。(当我取消对其中一部分的注释时,我看到了正确的stepper.tag)您确定整个阵列都更新了吗。使用alertView调试代码不是更好的方法,因为show方法在每个元素上都没有停止,而是堆叠它。用NSLog替换alertView并显示结果。