【问题标题】:Remove a MKPointAnnotation Object from a Mutable Array从可变数组中删除 MKPointAnnotation 对象
【发布时间】:2016-08-25 20:14:23
【问题描述】:

我有一个地图应用程序,它允许用户将注释作为收藏夹保存到可变数组中。然后可以在用户选择时显示所有喜欢的注释。

添加到可变数组的注解属于 MKPointAnnotation 类。我可以正确地向可变数组添加注释,但我还没有提出一个可行的解决方案来正确地从可变数组中删除特定注释。如何从包含多个已保存为收藏夹的注释的可变数组中删除特定注释?在我的示例代码中找到了几个不起作用的解决方案。

//** Correctly adds a favorite annotation to the mutable array favoriteAnnotationsArray **
-(void)addToFavoriteAnnotationsArray{
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    NSArray *components = [favoritesString componentsSeparatedByString:@","];
    annotation.coordinate = CLLocationCoordinate2DMake([components[1] doubleValue], [components[0] doubleValue]);
    annotation.title = [components[2] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];

    [self.favoriteAnnotationsArray addObject:annotation];

}
//** Need to remove a favorite annotation from the mutable array favoriteAnnotationsArray **
-(void)removeObjectFromFavoriteAnnotationsArray{

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    NSArray *components = [favoritesString componentsSeparatedByString:@","];
    annotation.coordinate = CLLocationCoordinate2DMake([components[1] doubleValue], [components[0] doubleValue]);
    annotation.title = [components[2] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];

    //** Used in first non-working solution below **
    //NSMutableArray *objectToRemoveArray = [[NSMutableArray alloc] init];
    //[objectToRemoveArray addObject:annotation];

    //** The following three lines didn't remove any annotations from array **
    //[self.favoriteAnnotationsArray removeObjectsInArray:objectToRemoveArray];
    //[self.favoriteAnnotationsArray removeObject:annotation];
    //[self.favoriteAnnotationsArray removeObjectIdenticalTo:annotation];

    //** This only removes the last object in array and not necessarily the correct annotation to remove **
    [self.favoriteAnnotationsArray removeLastObject];

}

【问题讨论】:

    标签: ios dictionary annotations


    【解决方案1】:

    一种方法是遍历注释数组并找到要删除的数组。例如:

    - (void)addToFavoriteAnnotationsArray:(NSString *)string {
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        NSArray *components = [string componentsSeparatedByString:@","];
        annotation.coordinate = CLLocationCoordinate2DMake([components[1] doubleValue], [components[0] doubleValue]);
        annotation.title = [components[2] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
    
        [self.favoriteAnnotationsArray addObject:annotation];
    }
    
    - (void)removeObjectFromFavoriteAnnotationsArray:(NSString *)string {
        NSArray *components = [string componentsSeparatedByString:@","];
        NSString *titleOfAnnotationToRemove = [components[2] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
        CLLocationCoordinate2D coordinateOfAnnotationToRemove = CLLocationCoordinate2DMake([components[1] doubleValue], [components[0] doubleValue]);
    
        for (NSInteger i = 0; i < self.favoriteAnnotationsArray.count; i++) {
            id<MKAnnotation>annotation = self.favoriteAnnotationsArray[i];
            if ([annotation.title isEqualToString:titleOfAnnotationToRemove] && coordinateOfAnnotationToRemove.latitude == annotation.coordinate.latitude && coordinateOfAnnotationToRemove.longitude == annotation.coordinate.longitude) {
                [self.favoriteAnnotationsArray removeObjectAtIndex:i];
                break;
            }
        }
    }
    

    或者,如果您想单独匹配标题,则从上面的 if 语句中删除坐标。另请注意,我已将字符串作为参数添加到这些方法中。将参数传递给方法总是比依赖某些属性更好。

    坦率地说,不过,当用户选择要删除的一个时,您可能不希望他们必须再次手动输入名称和坐标。您可能希望他们从列表中选择一个。因此,您可以向他们展示注释的表格视图,当他们说要删除第三个注释时,您只需将索引传递给这样的方法:

    - (void)removeObjectFromFavoriteAnnotationsArray:(NSInteger)index {
        [self.favoriteAnnotationsArray removeObjectAtIndex:index];
    }
    

    顺便说一句,上述所有例程都从您的数组中删除了注释。如果您还将此注释添加到地图视图中,请记住也将其从那里删除。

    【讨论】:

    • 非常感谢 Rob 的解决方案。我所有的注释标题都是唯一的,所以我简化了条件语句,只检查注释标题而不是坐标。顺便说一句,我不得不将条件中的 == 更改为 isEqualToString。再次感谢您的解决方案。
    • @Cheesehead1957 - 顺便说一句,我看到你问了很多问题,但似乎没有接受任何答案。请参阅What should I do when someone answers my question? 我不在乎您是否接受我的回答或aytunch 的回答,但您可能应该接受其中之一。您可能应该回顾一下您的问题历史,看看是否还有其他您应该接受的问题的答案。
    【解决方案2】:

    您需要从favoriteAnnotationsArray 中指定一个唯一的注释才能成功删除它。

    也许您可以尝试以下方法:

    -(void)removeAnnotationFromFavoriteAnnotationsArrayWithTitle: (NSString *) titleString {
        for(int i=0; i<self.favoriteAnnotationsArray.count; i++) {   
            MKPointAnnotation *annotation = (MKPointAnnotation *)[self.favoriteAnnotationsArray objectAtIndex:i];
            NSString * annotationTitle = annotation.title;
            if([annotationTitle isEqualToString:titleString]) {
                [self.favoriteAnnotationsArray removeObject:annotation];
                break;
            }
        }
    }
    

    如果标题不够独特,无法区分注释,那么您可以考虑继承 MKAnnotation 并添加唯一属性并将其传递给上述函数而不是标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2016-11-26
      • 2016-05-19
      相关资源
      最近更新 更多