簡體   English   中英

如何更改特定數組中的NSMutableArray

[英]how to alter NSMutableArray in an specific array

我有一個NSMutableArray,它擁有MKMapkit的Custom類。

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

我的例程填充了沒有圖像的placeObject ,因為它們將異步加載並在幾秒鍾后完成。 現在,我的問題是:有沒有辦法改變annotationArray placeObject中的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秒檢查一次是否已滿載。 然后,我開始更改注釋數組並刷新我的注釋(全部刪除->添加到地圖添加其他圖像)

- (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];
    }
}

您需要做的就是獲得對數組中MapPoint對象之一的引用。 然后,您可以根據需要在對象上設置屬性和/或調用方法。

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

您可以從可變數組中檢索要使用objectAtIndex方法更改的對象

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

您的MapPoint實例placeObject是一個指針。 因此,將其添加到數組中不會按值復制placeObject,而是實際上保留其地址。 從數組中獲取對象的地址將允許您對其進行操作。

annotationArray[objectIndex].anything = anything you want

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM