如何从表视图到地图视图显示注释?

时间:2012-08-07 06:09:01

标签: iphone objective-c ios

我有一个tableview,每行都有名字,每个标题都有不同的图像。我需要将这些标题传递到mapview,因为注释包含标题名称作为注释的标题。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
}

AddressInfo *addressInfo = [addressList objectAtIndex:indexPath.row];



NSString *str=[NSString stringWithFormat:@"%@",addressInfo.category];
NSString *str2=@"0";
if ([str isEqualToString:str2] ) {
    UIImage *image1=[UIImage imageNamed:@"greyDot.png"];
    cell.imageView.image=image1;
}
NSString *str3=@"10";
if ([str isEqualToString:str3] ) {
    UIImage *image2=[UIImage imageNamed:@"blackDot.png"];
    cell.imageView.image=image2;
}   
NSString *str4=@"20";
if ([str isEqualToString:str4] ) {
    UIImage *image3=[UIImage imageNamed:@"greendot.png"];
    cell.imageView.image=image3;
}   

NSString *str5=@"30";
if ([str isEqualToString:str5] ) {
    UIImage *image4=[UIImage imageNamed:@"blueDot.png"];
    cell.imageView.image=image4;
}

NSString *str6=@"1";
if ([str isEqualToString:str6] ) {
    UIImage *image5=[UIImage imageNamed:@"0Dot.png"];
    cell.imageView.image=image5;
}

cell.textLabel.text = addressInfo.name;
return cell;


}

但是在我的mapview中,我没有得到表格视图中的精确图像注释,我从表格视图中获取随机图像作为注释,但标题名称看起来与我想要的相同。

这是我的mapview代码:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
   pinView = nil;
if(annotation != mapview.userLocation) 
{

    static NSString *defaultPinID = @"defaultPinID";
    pinView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


    AddressInfo *address = [addressArray objectAtIndex:x];
NSString *dotstr=[NSString stringWithFormat:@"%@",address.category];
    NSString *dotstr1=@"0";
    if([dotstr isEqualToString:dotstr1])
    {
        NSLog(@"string 0 is %@",dotstr1);
        pinView.image=[UIImage imageNamed:@"greyDot.png"]; 
        NSLog(@"coming here 0");
    }
    NSString *dotstr2=@"10";
    if([dotstr isEqualToString:dotstr2])
    {
        NSLog(@"string 10 is %@",dotstr2);
        pinView.image=[UIImage imageNamed:@"blackDot.png"]; 
        NSLog(@"coming here 10");
    }
    NSString *dotstr3=@"20";
    if([dotstr isEqualToString:dotstr3])
    {
        NSLog(@"string 20 is %@",dotstr3);
        pinView.image=[UIImage imageNamed:@"greendot.png"];
        NSLog(@"coming here 20");
    }

    NSString *dotstr4=@"30";
    if([dotstr isEqualToString:dotstr4])
    {
        NSLog(@"string 30 is %@",dotstr4);
        pinView.image=[UIImage imageNamed:@"blueDot.png"];
        NSLog(@"coming here 30");
    }
    NSString *dotstr5=@"1";
    if([dotstr isEqualToString:dotstr5])
    {
        NSLog(@"string defau is %@",dotstr5);
        pinView.image=[UIImage imageNamed:@"greyDot.png"];
        NSLog(@"coming here default");
    }







    pinView.canShowCallout = YES;

    infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    infoButton.tag=k;
    [infoButton addTarget:self action:@selector(pinLabelClicked:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    x++;


}
else {
    [mapview.userLocation setTitle:@"I am here"];
}

return pinView;


}

在第一个屏幕中第一行包含每行左侧的蓝色图像我试图在地图视图中显示相同但在我的地图视图中蓝色注释显示灰色图像标题。 有谁知道如何做到这一点。任何教程或示例代码?

1 个答案:

答案 0 :(得分:0)

检查此框架https://github.com/jacobjennings/JJMapCallout

我在我的一个项目中使用它,它的工作很棒。 还要检查讨论herehere