我有两个观点。一个是地图视图,另一个是MKAnnotation
的所有标题的表格视图。现在,我可以通过点击MKAnnotation
来调用表格视图上的方法。 但我还要做的是通过点击表格视图中的对象来调用方法,该方法可以点击MKAnnotation
。请帮助吗?
以下是MKAnnotation
点按用于在表格视图中选择对象的表格视图代码。
- (void)highlightCellForPost:(PAWPost *)post {
// Find the cell matching this object.
NSUInteger index = 0;
for (PFObject *object in [self objects]) {
PAWPost *postFromObject = [[PAWPost alloc] initWithPFObject:object];
if ([post isEqual:postFromObject]) {
// We found the object, scroll to the cell position where this object is.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:PAWWallPostsTableViewMainSection];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
return;
}
index++;
}
以下是调用上述方法的MKAnnotation
的代码。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
id<MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[PAWPost class]]) {
PAWPost *post = [view annotation];
[self.wallPostsTableViewController highlightCellForPost:post];
} else if ([annotation isKindOfClass:[MKUserLocation class]]) {
// Center the map on the user's current location:
CLLocationAccuracy filterDistance = [[NSUserDefaults standardUserDefaults] doubleForKey:PAWUserDefaultsFilterDistanceKey];
MKCoordinateRegion newRegion = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate,
filterDistance * 2.0f,
filterDistance * 2.0f);
[self.mapView setRegion:newRegion animated:YES];
self.mapPannedSinceLocationUpdate = NO;
}
}
所以我基本上想做我上面所做的事情,但顺序相反。
答案 0 :(得分:0)
您只需在selectAnnotation
的地图视图中致电didSelectRowAtIndexPath
。
首先,我将创建一个新的NSArray属性postObjects
,它包含self.objects
中已转换为PAWPost
个对象的所有PFObject,而不必不断迭代对象数组并创建新的PAWPost对象。一旦你这样做,你的第一种方法变得更简单 -
- (void)highlightCellForPost:(PAWPost *)post {
// Find the cell matching this object.
NSUInteger index = [self.posts indexOfObject:post];
if (index != NSNotFound) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:PAWWallPostsTableViewMainSection];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
然后,你的tableview方法是
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == PAWWallPostsTableViewMainSection) {
PAWPost *post=self.posts[indexPath.row];
[self.mapView setCenterCoordinate:post.coordinate animated:YES];
[self.mapView selectAnnotation:post animated:YES];
}
}