0

我将一个包含对象的 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 的新屏幕。但正如我所说,我真的不确定从屏幕上的表格到显示位置对象的地图和用户的位置。

4

2 回答 2

0

使用 MapView 创建一个新的 ViewController 并将纬度和经度值传递给 New ViewController。

假设您已将视图控制器创建为 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

  • (id)initWithLocation:(位置 *) 位置;

在 MyLocationMapViewController.m

  • (id)initWithLocation:(Location *)location { *locationObj = location;

}

现在您可以使用 locationObj 获取地图值并在 MapView 中显示坐标。

于 2010-12-30T06:23:39.597 回答
0

在 secondview 控制器中创建 MapViewController 对象

在您的 secondview 控制器中编写以下代码

  • (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 的双变量来存储纬度和经度

于 2010-12-30T06:23:58.987 回答