Ios 无法将批注数据传递到tableviewcontroller
编辑问题: 我正在尝试获取在地图视图中创建的注释(带有标题/副标题),并将地图上的所有注释推送到一个显示带有标题/副标题列表的表视图中 我有一个RegionAnnotation.h/.m NSObject文件,用于反向地理编码,以填充MapViewController上的管脚。这个很好用。我做了一个长按,创建一个pin,标题和副标题显示出来,反向地理编码工作 现在我想将pin数据推送到tableview列表中。我已尝试调用Ios 无法将批注数据传递到tableviewcontroller,ios,uitableview,mkannotation,Ios,Uitableview,Mkannotation,编辑问题: 我正在尝试获取在地图视图中创建的注释(带有标题/副标题),并将地图上的所有注释推送到一个显示带有标题/副标题列表的表视图中 我有一个RegionAnnotation.h/.m NSObject文件,用于反向地理编码,以填充MapViewController上的管脚。这个很好用。我做了一个长按,创建一个pin,标题和副标题显示出来,反向地理编码工作 现在我想将pin数据推送到tableview列表中。我已尝试调用单元格中的区域注释信息,以获取单元格填充标题和副标题的正确格式。但是,当我
单元格中的区域注释信息,以获取单元格填充标题和副标题的正确格式。但是,当我调用以下命令时:
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;
我只得到标题,在这种情况下是“位置提醒”,但是,作为地址的字幕信息并没有填充到表中。所有显示的是文本“字幕”
如何使用标题和子文件信息填充单元格。我已经为此工作了一个多月,似乎找不到解决办法。请提供帮助。关于最新发布代码的一些评论:
CLPlacemark *pins = [self.annotations objectAtIndex:indexPath.row];
RegionAnnotation *annotation = [[RegionAnnotation alloc]
initWithLocationIdentifier:(NSString *)pins];
这看起来不对。pins
变量被声明为CLPlacemark
(这本身是可疑的,因为CLPlacemark
不符合MKAnnotation
,那么为什么它在“annotations”数组中?),然后它被转换为与CLPlacemark
没有关系的NSString*
。强制转换不会将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
这里的一个要点是,这个示例发送整个annotations数组。如果使用showsUserLocation=YES
显示用户的当前位置,则数组也将包含该注释。如果只想发送某些注释,则必须首先从地图视图数组中构建一个包含所需注释的新数组,并将plvc.annotations
设置为该新数组的值。一种简单的方法是循环查看mapView.annotations
,如果要包含注释,请使用addObject
将其添加到新数组中。直接使用地图视图的annotations
数组的另一个可能的问题是,如果地图上的注释在表视图仍显示注释列表时发生更改(添加/删除),它将失去同步,并可能导致运行时范围异常。为了避免这种情况,如有必要,您可以将plvc.annotations
设置为地图视图注释数组的副本(即[mapView.annotations copy]
)
- 在
PinListViewController
中,在numberofrowsin部分
方法中:
return self.annotations.count;
//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;
- 在
PinListViewController
中,在cellforrowatinexpath
方法中:
return self.annotations.count;
//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;
//这里的典型出列/alloc+init内容。。。
//假设单元格样式设置为UITableViewCellStyleSubtitle
id annotation=[self.annotations objectAtIndex:indexath.row];
cell.textlab.text=annotation.title;
cell.detailTextLabel.text=annotation.subtitle;
返回单元;
注释
声明为id
,因此它可以用于任何类型的注释,因为示例只需要显示标准的标题
和副标题
属性。如果需要显示自定义注释类中的自定义属性,可以使用isKindOfClass
检查annotation
是否属于该类型,然后可以将其强制转换为该自定义类并引用自定义属性
在视图中加载行[self.annotations addObjectsFromArray:_annotations]代码>没有意义,因为_annotations是annotations属性后面的ivar,所以本质上它们指向相同的东西。如果表格应该显示管脚,为什么numberOfRowsInSection返回regions.count?区域与注释有什么关系?在cellForRowAtIndexPath中,为什么要创建一个新的RegionAnnotation对象?关于viewDidLoad,您是对的,我想删除它,谢谢。我有一个名为RegionAnnotation的.m文件,其中包含调用该信息的代码。我将编辑原始注释以显示。好的,但是如果地图视图控制器上已经有注释,为什么需要在cellForRowAtIndexPath中创建注释的新实例?地图上的注释不是已经设置了标题和副标题吗?仍然不清楚为什么在PinListVC中同时有区域和注释。地图VC如何创建和显示pin列表VC?是的,当选择pin时,地图视图显示标题和副标题。我只是不知道如何让这些信息以列表的形式显示在桌子上。我想,既然我正在调用RegionAnnotation文件中的信息以在mapview上显示,那么我需要在pinlistVC上调用相同的信息。这就是我被困的地方。我不必创建新实例,只需传递信息即可。然而,我不知道该怎么做。如果我从viewDidLoad中删除区域计数,则我的tableview为空