Reputation: 3600
I am trying to create a table view that shows a list of my map annotations. Each cell would contain the name(title) and address(subtitle). I'm then going to add a search bar to the table view. When I try to add the annotation titles and subtitles to an array for my table view, the array never gets any objects added to it. It's count is still 0. Any help?
Here is how I'm adding the annotations to my map view:
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];
I have tried adding this to the loop to add the title to my table view array but the array stays empty.
[list.marketList addObject:marketAnnotation.title];
EDIT: More code.
RSFM is my map view. List is my table view.
In RSFM, here is where I add the annotations to my map and add the annotation titles to the marketList array in List.
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
Upvotes: 0
Views: 517
Reputation: 8320
Probably your array is not initialized. Initialize it above 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];
Upvotes: 1