【问题标题】:How can I make a method act as a tap on a MKAnnotation?如何使方法充当 MKAnnotation 的点击?
【发布时间】:2014-12-28 06:03:07
【问题描述】:

我有两种观点。一个是地图视图,另一个是MKAnnotations 的所有标题的表格视图。现在我可以通过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;
}

}

所以我基本上想做我上面所做的事情,但顺序相反。

【问题讨论】:

  • 只需在您的didSelectRowAtIndexPath 中调用[mapView selectAnnotation:post]
  • 我无法从我的 tableView 中调用 mapView,因为它们位于不同的 mapview 中,并且位于 tableView 是其子视图的 viewController 中。有什么办法可以解决吗?

标签: ios objective-c dictionary annotations mapkit


【解决方案1】:

您可以在didSelectRowAtIndexPath 的地图视图中简单地调用selectAnnotation

首先,我将创建一个新的 NSArray 属性 postObjects,其中包含已转换为 PAWPost 对象的 self.objects 中的所有 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];
    }
}

【讨论】:

  • 我没有提到 mapView 在我的 tableView 是子视图的 viewController 中。所以我无法从表视图访问 mapView。我该如何解决这个问题?
  • 首先我会考虑有一个单一的视图控制器,但否则你需要在你的表视图控制器中设置一个属性来保存对你的 mapView 的引用——就像你在你的恶意视图控制器中所做的那样为您的 tableView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多