Reputation: 1024
In my mapview project, I have passed an NSMutableArray of my custom annotation to a table view. in tha mapview .m code:
MyLocation *nota =[[MyLocation alloc] initWithName:name time:horario];
[_mapView addAnnotation:nota]; //add location to map
[mapViewAnnotations addObject:nota]; //copy "nota" to the new array...
}
TablaViewController *tablaController =[[TablaViewController alloc]initWithNibName:@"TablaViewController" bundle:nil];
tablaController.locationFromMap = mapViewAnnotations; //... then I pass array to table
in the table View:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Locations";
listOfItems = [[NSMutableArray alloc] init];
for (int i = 0; i < [locationFromMap count]; i++){
MyLocation *location = [locationFromMap objectAtIndex:i];
//Add items
[listOfItems addObject:location.name];
NSLog(@" list count : %d",[listOfItems count]);
}
}
where locationFromMap is a NSmtable array of custom location with some atributes like name, subname etc.
Using :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
location "name" are correctly showed in the table. My question is how can a come back to map focusing on a concrete location when the relative row is clicked. Perarps my method is wrong ?
Thanks in advance.
Upvotes: 0
Views: 279
Reputation: 50109
so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyLocation *loc = [locationFromMap objectAtIndex:indexPath.row];
MKCoordinateRegion r = MKCoordinateRegionMake(loc.coordinate, MKCoordinateSpanMake(0.005, 0.005));
[self.mapView setRegion:r animated:YES];
}
(typed inline but something like this)
Upvotes: 1