TableView显示地图注释标题和字幕

时间:2013-06-18 13:09:56

标签: ios uitableview mkmapview mkannotation

我正在尝试创建一个表格视图,其中显示了我的地图注释列表。每个单元格将包含名称(标题)和地址(副标题)。然后我要在表格视图中添加一个搜索栏。当我尝试将注释标题和副标题添加到我的表视图的数组时,该数组永远不会添加任何对象。它的数量仍为0.任何帮助?

以下是我如何将注释添加到地图视图中:

for (int x = 0; x < [lat count]; x++)
    {
        marketAnnotation = [[MKPointAnnotation alloc]init];
        location.latitude = [[lat objectAtIndex:x]floatValue];
        location.longitude = [[lon objectAtIndex:x]floatValue];
        marketAnnotation.coordinate = location;
        marketAnnotation.title = [title1 objectAtIndex:x];
        marketAnnotation.subtitle = [subtitle1 objectAtIndex:x];
        [marketLocations addObject:marketAnnotation];
    }

    [worldView addAnnotations:marketLocations];

我尝试将其添加到循环中以将标题添加到我的表视图数组中,但数组保持为空。

[list.marketList addObject:marketAnnotation.title];

编辑:更多代码。

RSFM是我的地图视图。列表是我的表视图。

在RSFM中,我在这里将注释添加到我的地图中,并将注释标题添加到List中的marketList数组。

List *list = [[List alloc]init];
    list.marketList = [[NSMutableArray alloc]init];

    for (int x = 0; x < [lat count]; x++)
    {
        marketAnnotation = [[MKPointAnnotation alloc]init];
        location.latitude = [[lat objectAtIndex:x]floatValue];
        location.longitude = [[lon objectAtIndex:x]floatValue];
        marketAnnotation.coordinate = location;
        marketAnnotation.title = [title1 objectAtIndex:x];
        marketAnnotation.subtitle = [subtitle1 objectAtIndex:x];
        [marketLocations addObject:marketAnnotation];

        [list.marketList addObject:marketAnnotation.title];
    }

    [worldView addAnnotations:marketLocations];

    NSLog(@"List Count: %d", [list.marketList count]);

List.h

@interface List : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
{
    IBOutlet UISearchBar *searchBar;
}

@property (nonatomic, retain) NSMutableArray *marketList;

@end

List.m

#import "List.h"
#import "RSFM.h"
#import "DTCustomColoredAccessory.h"

@interface List ()

@end

@implementation List
{

}

@synthesize marketList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    NSLog(@"List Count: %d", [marketList count]);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [marketList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *marketListIdentifier = @"SimpleTableItem";
    UIImageView *image = [[UIImageView alloc]init];
    image.image = [UIImage imageNamed:@"CellImage.png"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:marketListIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:marketListIdentifier];
        cell.textLabel.font=[UIFont systemFontOfSize:16.0];
    }

    cell.textLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundView = image;

    NSLog(@"cell separator style: %d separator color: %@", tableView.separatorStyle, tableView.separatorColor);

    cell.textLabel.text = [marketList objectAtIndex:indexPath.row];

    DTCustomColoredAccessory *accessory = [DTCustomColoredAccessory accessoryWithColor:cell.textLabel.textColor];
    accessory.highlightedColor = [UIColor darkGrayColor];
    cell.accessoryView =accessory;

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:1)

可能您的阵列未初始化。 将其初始化为循环:

marketLocations = [[NSMutableArray alloc] init];

for (int x = 0; x < [lat count]; x++)
{
    marketAnnotation = [[MKPointAnnotation alloc]init];
    location.latitude = [[lat objectAtIndex:x]floatValue];
    location.longitude = [[lon objectAtIndex:x]floatValue];
    marketAnnotation.coordinate = location;
    marketAnnotation.title = [title1 objectAtIndex:x];
    marketAnnotation.subtitle = [subtitle1 objectAtIndex:x];
    [marketLocations addObject:marketAnnotation];
}

[worldView addAnnotations:marketLocations];