【问题标题】:TableView Showing Map Annotation Titles and Subtitles显示地图注释标题和副标题的 TableView
【发布时间】:2013-06-18 13:09:55
【问题描述】:

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

【问题讨论】:

    标签: ios uitableview mkmapview mkannotation


    【解决方案1】:

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

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

    【讨论】:

    • 我初始化了 list.marketList 并且计数是应该的,但是现在当我单击我的栏按钮以显示表格视图时,它是空的。该数组的计数回到 0。
    • @user2029585 这可能是因为数组对于该方法是本地的,并且您尝试在方法之外使用它。如果问题仍然存在,则需要查看更多代码。
    • @user2029585 这是忽略设置您的表视图'a 委托(在 IB 或代码中)的症状。或者在将数据添加到数组后调用 reloadData 失败(如果您正在异步加载数组)。
    • @user2029585 数据源设置了吗?
    • 你使用List.m中的表格吗?您在RSFM 中初始化数组,而您使用该数组在List.m 中加载表。如果您在RSFM 中使用表,则将表委托和数据源方法转移到RSFM 并在RSFM 中实现UITableViewDelegateUITableDataSource
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多