2013-06-18 21 views
0

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

这里是我如何添加注释,我的地图视图:

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中,我在这里添加注释到我的地图,并将注释标题添加到列表中的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

可能是你数组未初始化。上述 初始化它的循环:

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]; 
+0

我初始化list.marketList和计数是应该的,但现在,当我点击我的栏按钮,显示表视图,它是空的。该数组的数量回到0. – raginggoat

+0

@ user2029585这可能是因为数组对于该方法是本地的,并且您尝试在方法外使用它。如果问题仍然存在,那么需要查看更多的代码。 – Geek

+0

我已添加更多代码。 – raginggoat