我有一个视图控制器,它包含一个mapView和tableView(如下图所示)。我想知道是否可以点击表格单元格,mapView区域会更新其坐标?我尝试过很多方法,主要方法看起来很有希望:
HorizontalTableCell.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *currentSelected = [_articles objectAtIndex:indexPath.row];
HomeViewController *mvc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
CLLocationCoordinate2D location;
location.latitude = [[currentSelected objectForKey:@"lat"] doubleValue];
location.longitude = [[currentSelected objectForKey:@"long"] doubleValue];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 1.5*METERS_PER_MILE, 1.5*METERS_PER_MILE);
[mvc.mapView setRegion:region];
//testing, shows which table cell is selected
NSString *msg = [NSString stringWithFormat:@"%f %f", location.latitude, location.longitude];
UIAlertView *messageAlert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
}
tableView使用自定义表格单元格,因此此方法位于自己的HorizontalTableCell类中(而mapView位于HomeViewController类中)。选择一个单元格是有效的,因为警报为我提供了所选单元格的正确坐标(从数组_articles中获取lat和long),但由于某种原因,我无法在主视图控制器中访问或控制我的mapView。 / p>
我尝试的另一种方法是在Horizontal Table Cell的didSelectRowAtIndexPath中调用Home View Controller方法:
HomeViewController.h
- (void)changeMapRegion:(CLLocationCoordinate2D *)coord;
HomeViewController.m
- (void)changeMapRegion:(CLLocationCoordinate2D)coord {
MKCoordinateRegion newRegion = MKCoordinateRegionMakeWithDistance (coord, 1.5*METERS_PER_MILE, 1.5*METERS_PER_MILE);
NSLog(@"the coordinates are %f and %f", coord.latitude, coord.longitude);
[_mapView setRegion:newRegion animated:YES];
}
在HorizontalTableCell.m
中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *currentSelected = [_articles objectAtIndex:indexPath.row];
HomeViewController *mvc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
CLLocationCoordinate2D location;
location.latitude = [[currentSelected objectForKey:@"lat"] doubleValue];
location.longitude = [[currentSelected objectForKey:@"long"] doubleValue];
[mvc changeMapRegion:location];
}
当我点击一个表格单元格时,日志会给我正确的坐标,所以我调用方法很好,但mapView不会改变它的区域..我完全卡住了!我会喜欢一些建议。