2013-06-18 23 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