2013-05-02 20 views
0

我有一個NSMutableArray,它擁有一個MKMapkit的Custom類。如何修改特定數組中的NSMutableArray

MapPoint *placeObject = [[MapPoint alloc] initWithTitle:title subtitle:subtitle coordinate:loc description:description storeImage:storeImage]; 
[annotationArray addObject:placeObject]; 

我的日常充滿placeObject沒有圖像,因爲他們將異步加載和幾秒鐘後完成。現在我的問題是:有沒有辦法改變annotationArray內的placeObject

編輯

爲了在這裏更好的方式顯示我的問題有些部分代碼:

if (annotationArray == nil) 
{ 
    [self setAnnotationArray:[[NSMutableArray alloc] init]]; 
} 
for (NSDictionary *locationDetails in parser.items) 
{ 
    __block NSData *storeImageData; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); 
    dispatch_async(queue, ^{ 
     storeImageData = [NSData dataWithContentsOfURL:storeImageURL]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 
      UIImage *storeImageTMP = [UIImage imageWithData:storeImageData]; 
      counterBlock ++; 
      NSLog(@"async: %d", counterBlock); 
      [imageArray addObject:storeImageTMP]; 
      }); 
     }); 
MapPoint *placeObject = [[MapPoint alloc] initWithTitle:title subtitle:subtitle coordinate:loc description:description storeImage:storeImage]; 
[annotationArray addObject:placeObject]; 
} 
[mapView addAnnotations:annotationArray]; 

這樣做後:dispatch_queue_t將開始工作。所以我檢查每2秒,如果它的完全加載。然後,我開始改變annotationArray並刷新我的註釋(刪除所有 - >添加到地圖魔女附加圖像)

- (void) checkMap 
{ 
    if (counterBlock == 547) 
    { 
     for (int i=0; i<=counterBlock; i++) 
     { 
      MapPoint *point = annotationArray[i]; 
      point.storeImage = imageArray[i]; 
     } 
     if(timer) 
     { 
      [timer invalidate]; 
      timer = nil; 
     } 
     [mapView removeAnnotations:mapView.annotations]; 
     [mapView addAnnotations:annotationArray]; 
    } 
} 
+0

只要改變它。它是存儲在數組中的指針,不是placeObject的副本。無論你對原始數據做什麼「將出現」在通過數組訪問的版本中(因爲它們將是同一個對象)。 – 2013-05-02 17:00:39

回答

4

所有你需要做的就是到陣列中的MapPoint對象中的一個參考。然後,您可以根據需要在對象上設置屬性和/或調用方法。

MapPoint *point = annotationArray[x]; // x is whatever index you need 
point.image = ... // some image 
+0

我不知道爲什麼,但它不工作我總是得到'(null)' – CTSchmidt 2013-05-03 13:33:11

+0

你確定'annotationArray'不是'nil'嗎? – rmaddy 2013-05-03 14:30:21

+0

我更新了我的問題'annotationArray'不是'nil',只有圖像的空間 – CTSchmidt 2013-05-03 14:50:29

0

您可以從可變數組檢索,你想用objectAtIndex方法

NSInteger indexofMyObject = 0; 
MapPoint *point = [myArray objectAtIndex:indexofMyObject]; 
0

你的MapPoint實例placeObject改變對象的指針。因此,將它添加到數組中並不會按值複製placeObject,而是實際保留其地址。從數組中獲取對象的地址將允許您操作它。

annotationArray[objectIndex].anything = anything you want