0

我有一个 NSMutableArray,它拥有一个自定义类的 MKMapkit。

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

我的例程填充placeObject没有图像,因为它们将异步加载并在几秒钟后完成。placeObject现在我的问题是:有没有办法改变annotationArray?

编辑

为了更好地显示我的问题,这里有一些代码部分:

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

3 回答 3

4

您需要做的就是获取MapPoint对数组中一个对象的引用。然后,您可以根据需要在对象上设置属性和/或调用方法。

MapPoint *point = annotationArray[x]; // x is whatever index you need
point.image = ... // some image
于 2013-05-02T15:57:25.997 回答
0

您可以从可变数组中检索要使用该objectAtIndex方法更改的对象

NSInteger indexofMyObject = 0;
MapPoint *point = [myArray objectAtIndex:indexofMyObject];
于 2013-05-02T16:02:58.317 回答
0

您的 MapPoint 实例 placeObject 是一个指针。所以将它添加到数组中并不会按值复制 placeObject,而是实际上保存了它的地址。从数组中获取对象的地址将允许您对其进行操作。

annotationArray[objectIndex].anything = anything you want
于 2013-05-02T16:36:42.527 回答