我正在将包含对象的NSArray传递到屏幕,其中对象属性显示在表格中。这些对象中的每一个都包含纬度属性和经度属性。我想实现一个功能,用户选择一个单元格(其中每个单元格代表NSArray中的一个对象),然后用户被带到另一个屏幕,在那里他们可以看到一个表示对象在地图上的位置的注释,以及表示用户的注释。我该怎么做呢?这是我的RootViewController.m类的相关代码:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
self.secondViewController = controller;
[controller release];
self.secondViewController.locationList = sortedLocations;
[[self navigationController] pushViewController:controller animated:YES];
我在SecondViewController.m中的相关代码如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"locationcell";
LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Location *location = [locationList objectAtIndex:indexPath.row];
cell.locationName.text = location.name;
cell.locationAddress.text = location.address;
cell.locationDistance.text = location.distance;
return cell;
}
请注意,可见属性是名称,地址和距离,但位置对象还包含纬度和经度属性。我知道我必须创建一个名为MapViewController的新屏幕。但正如我所说,我真的不知道从屏幕上的表格到显示位置对象的地图和用户。
答案 0 :(得分:0)
使用MapView创建一个新的ViewController,并将纬度和经度值传递给New ViewController。
假设您已将视图Controller创建为MyLocationMapViewController。
在SecondViewController.m中实现以下
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Location *location = [locationList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:[[MyLocationMapViewController alloc] initWithLocation:location] animated:YES];
}
在MyLocationMapViewController.h
中
}
现在,您可以使用locationObj获取地图值并在MapView中显示坐标。
答案 1 :(得分:0)
在secondview controller中创建MapViewController对象
在第二个视图控制器中编写以下代码
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MapViewController * mapView = [[MapViewController alloc] initWithNibName:@“MapViewController”bundle:[NSBundle mainBundle]];
self. mapView = controller;
[mapView release];
self.mapView.latit = location.latitude;
self.mapView.longi = location.longitude;
[[self navigationController] pushViewController:controller animated:YES];
}
在MapViewController中创建2个名为latit和longi的双变量来存储纬度和经度