【问题标题】:Unable to pass annotation data to tableviewcontroller无法将注释数据传递给 tableviewcontroller
【发布时间】:2012-08-24 18:38:56
【问题描述】:

编辑问题:

我正在尝试获取在地图视图中创建的注释(带有标题/副标题)并将我的地图上的所有注释推送到显示带有标题/副标题的列表的表格视图。

我有一个 RegionAnnotation.h/.m NSObject 文件,它可以进行反向地理编码,我需要填充 MapViewController 上的引脚。这工作得很好。我长按创建一个图钉,然后显示标题和副标题,反向地理编码工作。

现在我想将 pin 数据推送到 tableview 列表。我尝试在cellForRowAtIndexPath 中调用区域注释信息,并使用UITableViewCellStyleSubtitle 来获取单元格的正确格式以填充标题和副标题。但是,当我调用以下命令时:

if (cell == nil){
    NSLog(@"if cell");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

CLPlacemark *pins = [self.annotations objectAtIndex:indexPath.row];
RegionAnnotation *annotation = [[RegionAnnotation alloc] initWithLocationIdentifier:(NSString *)pins];
cell.textLabel.text = annotation.title;

我只得到标题,在这种情况下是“位置提醒”,但是,作为地址的字幕信息不会填充表格。只显示文本“副标题”。

如何使用标题和副标题信息填充单元格。我已经为此工作了一个多月,似乎无法找到解决方案。请帮忙。

【问题讨论】:

  • 在 viewDidLoad 中,[self.annotations addObjectsFromArray:_annotations]; 行没有意义,因为 _annotations 是 annotations 属性后面的 ivar,所以它们基本上指向同一个东西。如果表格应该显示引脚,为什么 numberOfRowsInSection 返回regions.count?区域与注释有什么关系?在 cellForRowAtIndexPath 中,为什么要新建一个 RegionAnnotation 对象?
  • 您对 viewDidLoad 的看法是正确的,我打算删除它,谢谢。我有一个名为 RegionAnnotation 的 .m 文件,其中包含调用信息的代码。我将编辑原件以显示。
  • 好的,但是如果您已经在地图视图控制器上拥有注释,为什么需要在 cellForRowAtIndexPath 中创建它们的新实例?地图VC上的注释不是已经设置了标题和副标题吗?仍然不清楚为什么 PinListVC 中有区域和注释。 map VC如何创建并显示PinListVC?
  • 是,MapView选择了PIN时显示标题和字幕。我只是不知道如何让这些信息显示为表格上的列表。我想既然我正在调用 RegionAnnotation 文件中的信息以显示在地图视图上,那么我需要在 pinlistVC 上调用相同的信息。这就是我卡住的地方。我不应该创建新实例,只需传递信息。但是,我不知道该怎么做。如果我从 viewDidLoad 中删除区域计数,那么我的 tableview 是空白的。
  • 我删除了 [self.annotations addObjectsFromArray:_annotations];仍然卡住。还有其他建议吗?

标签: ios uitableview mkannotation


【解决方案1】:

一些cmets关于最新代码贴出:

CLPlacemark *pins = [self.annotations objectAtIndex:indexPath.row];
RegionAnnotation *annotation = [[RegionAnnotation alloc] 
    initWithLocationIdentifier:(NSString *)pins];

这看起来不对。 pins 变量被声明为 CLPlacemark (这本身很可疑,因为 CLPlacemark 不符合 MKAnnotation 那么为什么它在“注释”数组中?)然后它被强制转换为 @ 987654326@ 与CLPlacemark 无关。演员表不会将pins 转换为字符串——它会将pins 指向的数据视为NSString(它不是)。

之前发布的代码还有太多其他问题、疑问和未知数。


相反,我将举例说明如何将地图视图上的注释传递给表格视图并在单元格中显示数据......
  • PinListViewController(带有表格视图的那个)中,我们声明了一个NSArray 属性来接收和引用注释:

    //in the .h:
    //Don't bother declaring an ivar with the same name.
    @property (nonatomic, retain) NSArray *annotations;
    //in the .m:
    @synthesize annotations;
    
  • 接下来,在MapViewController,在你想要展示/推送/显示PinListViewController的地方,代码会是这样的:

    PinListViewController *plvc = [[PinListViewController alloc] init...
    
    //pass the map view's annotations array...
    plvc.annotations = mapView.annotations;
    
    [self presentModalViewController:plvc animated:YES];  //or push, etc.
    
    [plvc release];  //remove if using ARC
    

    这里很重要的一点是,这个例子发送了整个注解数组。如果您使用showsUserLocation = YES 显示用户的当前位置,那么该数组也将包含该注释。如果您只想发送某些注释,则必须首先从地图视图数组中构建一个包含您想要的注释的新数组,并将plvc.annotations 设置为等于该新数组。一种简单的方法是循环遍历mapView.annotations,如果要包含注释,则使用addObject 将其添加到新数组中。直接使用地图视图的annotations 数组的另一个可能的问题是,如果地图上的注释更改(添加/删除)表格视图仍在显示注释列表,它将消失同步并可能导致运行时范围异常。为避免这种情况,如有必要,您可以将plvc.annotations 设置为地图视图的注释数组的副本(即[mapView.annotations copy])。

  • PinListViewController 中,在numberOfRowsInSection 方法中:

    return self.annotations.count;
    
  • PinListViewController 中,在cellForRowAtIndexPath 方法中:

    //typical dequeue/alloc+init stuff here...
    //assume cell style is set to UITableViewCellStyleSubtitle
    
    id<MKAnnotation> annotation = [self.annotations objectAtIndex:indexPath.row];
    cell.textLabel.text = annotation.title;
    cell.detailTextLabel.text = annotation.subtitle;
    
    return cell;
    

    annotation 被声明为id&lt;MKAnnotation&gt;,因此它适用于任何类型的注释,因为该示例只需要显示标准的titlesubtitle 属性。如果您需要显示自定义注释类中可能具有的自定义属性,您可以使用isKindOfClass 检查annotation 是否属于该类型,然后您可以将其强制转换为该自定义类并引用自定义属性。

【讨论】:

  • 谢谢你,安娜,在我看到这个之前,我做了一些与你的回答非常相似的事情。我无法得到的一件事是要加载的标题,但现在地址正在填充 tableviewcontroller。我将查看上述内容并添加任何可能有帮助的内容,稍后再发布以让您知道它有效。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多