从表视图中的选定行打开地图注释 [英] Open Map Annotation from Selected Row in Table View

查看:77
本文介绍了从表视图中的选定行打开地图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个班级中有一个地图视图,其中包含所有带标题和副标题的注释。有一个导航栏按钮,显示所有这些标题和副标题的列表。我希望能够在表视图中选择一行并让它在地图视图中打开具有相同标题的注释。有什么帮助?

I have a map view in one class with all of my annotations with titles and subtitles. There is a nav bar button that shows a list of all of those titles and subtitles. I'm wanting to be able to select a row in the table view and have it open the annotation with the same title in the map view. Any help?

我现在唯一的问题是在地图视图打开时显示注释。我知道我需要使用selectAnnotation:animated:但是我无法弄清楚如何告诉它打开带有匹配标题的注释。

The only problem I have now is getting the annotation to show when the map view opens. I know I need to use selectAnnotation: animated: but I'm having trouble figuring out how to tell it to open the annotation with the matching title.

在RSFM中(类使用名为worldView的地图视图)我显示如下表格视图:

In RSFM (class with map view named worldView) I show the table view like this:

- (void)showList
{
    List *list = [[List alloc] initWithNibName:nil bundle:nil];
    list.annotations = [[NSArray alloc]initWithArray:worldView.annotations];
    list.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:list animated:YES];
}

以下是我在表格视图中的didSelectRowAtIndexPath方法中的内容:

Here is what I have so far in my didSelectRowAtIndexPath method in my table view:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil];

        NSLog(@"List Annotations Array Count: %d", [annotations count]);

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellTitle = selectedCell.textLabel.text;
        NSLog(@"Cell Title: %@", cellTitle);


        for (NSUInteger i = 0; i < [annotations count]; i++)
        {
            NSString *stringFromArray = [[annotations objectAtIndex:i]title];
            NSLog(@"Annotations string: %@", stringFromArray);

            if ([cellTitle isEqualToString:stringFromArray])
            {
                RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil];
                rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
                [self.navigationController pushViewController:rsfm animated:YES];

                // [rsfm.worldView selectAnnotation:<#(id<MKAnnotation>)#> animated:YES];
            }
        }

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }


推荐答案

当你选择一行时 tableView ,调用您的方法 tableView:didSelectRowAtIndexPath:。所以你必须在那里使用 indexPath 来读取你的模型中的数据,并获得标题。

然后你可以从你的<读取所有注释code> mapView ,使用其注释属性。在您获得的数组中,扫描对象,直到找到具有相同标题的注释。然后你可以发送 mapView 消息 selectAnnotation:animated:,它会显示出来。

请注意,有更聪明的方法可以解决这个问题,但是第一次尝试它应该可行。

When you select a row in the tableView, your method tableView:didSelectRowAtIndexPath: is called. So you have to use there the indexPath to read the data from your model, and get the title.
You could then read all annotations from your mapView, using its annotations property. In the array that you get, scan the objects until you find the annotation which has the same title. Then you can send your mapView the message selectAnnotation:animated:, and it will be shown.
Please be aware that there are more clever approaches to this problem, but for a first try it should work.

这篇关于从表视图中的选定行打开地图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆