点击单元格时,应添加注释

时间:2013-07-15 06:24:24

标签: ios uitableview mkannotation

我需要在触摸tableview的任何单元格时向地图视图添加注释。我有一个方法,我添加注释viewForAnnotation和方法didSelectRowAtIndexPath。

我无法建立逻辑连接,在哪里调用方法以及如何调用?

你能帮我吗?

viewForAnnotation方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id  <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
    return nil;
}
else if ([annotation isKindOfClass:[CustomAnnotation class]])
{
    static NSString * const identifier = @"MyCustomAnnotation";

    MKAnnotationView* annotationView = [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView)
    {
        annotationView.annotation = annotation;
    }
    else
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
    }

    UIImageView * ecz = [[UIImageView alloc]init];

    ecz.frame = CGRectMake(0, 0, 65, 49);

    ecz.image = [UIImage imageNamed:@"indicator.png"];


    UIImageView * vstd = [[UIImageView alloc]init];

    vstd.frame = CGRectMake(33, 10, 24, 22);

    vstd.image = [UIImage imageNamed:@"indicator_ziyaret_gri"];


    UIImageView * nbox = [[UIImageView alloc]init];

    nbox.frame = CGRectMake(48, -6, 22, 22);

    nbox.image = [UIImage imageNamed:@"numara_kutusu"];

    [annotationView addSubview:ecz];

    [annotationView addSubview:vstd];


    UILabel *index = [[UILabel alloc] initWithFrame:CGRectMake(5,4,15,15)];
    index.text = @"1";
    index.textColor = [UIColor whiteColor];
    [index setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
    [nbox addSubview:index];
    [index setBackgroundColor:[UIColor clearColor]];

    [annotationView addSubview:nbox];

    annotationView.canShowCallout = YES;

    return annotationView;
}

return nil;
}

didSelectRowAtIndexPath方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EczaneCell *cell = [tableView cellForRowAtIndexPath:indexPath];

 }

1 个答案:

答案 0 :(得分:2)

mapView: viewForAnnotation:方法是MapView的delegate方法,当您ADDREMOVE来自mapview的任何注释时,MapView内部会调用此方法。

要在TableView点击添加注释,您应该在tableView:didSelectRowAtIndexPath:

中执行以下操作
[self.map addAnnotaion:<YOUR Annotation>];