0

我正在尝试创建一个显示我的地图注释列表的表格视图。每个单元格将包含名称(标题)和地址(副标题)。然后,我将在表格视图中添加一个搜索栏。当我尝试将注释标题和副标题添加到我的表格视图的数组中时,该数组永远不会添加任何对象。它的计数仍然是 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]);

列表.h

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

@property (nonatomic, retain) NSMutableArray *marketList;

@end

列表.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
4

1 回答 1

1

可能您的数组未初始化。在 for loop 上面初始化它:

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];
于 2013-06-18T13:13:46.050 回答