使用didSelectRowAtIndexPath:
传递一些数据,从一个table视图控制器到一个detail视图控制器。
我现在要将数据从地图callOutAccessoryControlTapped:
方法发送出去,但是不知道应该怎么实现。
应该怎么用callOutAccessoryControlTapped:
发送detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
?
其中objectAtIndex:indexPath.row
是不在地图callOutAccessoryControlTapped:
方法中辨识的。
下面是didSelectRowAtIndexPath:
代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.latStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"lat"];
detailViewController.lngStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"lng"];
detailViewController.addressStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"address"] objectForKey:@"address"];
detailViewController.titleStr = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
detailViewController.mainImageUrl = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"];
detailViewController.listingId = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"id"];
detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
下面是地图注释calloutAccessoryControlTapped:
方法:
- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:@"ScrollView_ExampleViewController" bundle:nil];
MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;
detailViewController.titleStr = theAnnotation.title;
detailViewController.addressStr = theAnnotation.subtitle;
// detailViewController.latStr = theAnnotation.latString;
// detailViewController.lngStr = theAnnotation.lngString;
// detailViewController.url = theAnnotation.theUrl;
[self.navigationController pushViewController:detailViewController animated:YES];
}
在 callOutAccessoryControlTapped: 方法中,你可以使用相同的方式来传递数据给 detailViewController,就像 didSelectRowAtIndexPath: 方法中所做的那样。但是因为这个方法中并没有indexPath这个参数,所以不能使用 objectAtIndex:indexPath.row 的方法来获取数据。
解决方案是,你可以在添加 MyAnnotation 的时候,将需要传递给 detailViewController 的数据附加到 MyAnnotation 对象中。然后在 callOutAccessoryControlTapped: 方法中,获取 MyAnnotation 对象中的数据并传递给 detailViewController。
例如,你可以在 MyAnnotation 中添加属性 descriptionTextViewString,然后在添加 MyAnnotation 到地图时将数据赋值给这个属性,在 callOutAccessoryControlTapped: 方法中获取该属性并传递给 detailViewController.descriptionTextViewString。
(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *aV in views) {
//check if annotation is of class MyAnnotation
if ([aV.annotation isKindOfClass:[MyAnnotation class]]) {
MyAnnotation *myAnnotation = (MyAnnotation *)aV.annotation;
myAnnotation.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
}
}
}
在callOutAccessoryControlTapped: 方法中
MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;
detailViewController.descriptionTextViewString = theAnnotation.descriptionTextViewString;
这样就可以在callOutAccessoryControlTapped: 方法中传递descriptionTextViewString 这个数据给 detailViewController 了。