【问题标题】:Annotations and Table Views注释和表格视图
【发布时间】:2012-08-16 12:09:51
【问题描述】:

我正在尝试使用我的注释数组填充我的表格视图,但 XCode 似乎在我添加此代码时给了我一个断点。

if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSMutableArray *annotations = [[NSMutableArray alloc] init];

if(indexPath.section == 0)
{
    for(Location *annotation in [(MKMapView *)self annotations])
    {
        if(![annotation isKindOfClass:[MKUserLocation class]])
        {
    }
    }
    cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
}
return cell;

我的注释:

CLLocationCoordinate2D thecoordinate59;

thecoordinate59.latitude = 51.520504;
thecoordinate59.longitude = -0.106725; 
Location *ann1 = [[Location alloc] init];
ann1.title =@"Antwerp";
ann1.coordinate = thecoordinate1;

NSMutableArray *annotations = [NSMutableArray arraywithObjects: ann.. ann59, nil];

[map addAnnotations:annotations];

【问题讨论】:

  • 在这里发布整个tableView:cellForRowAtIndexPath: 方法。这有点乱。
  • for(Location *annotation in [(MKMapview *)self annotations 上的断点
  • 控制台说你有什么错误? (断点不是错误)。
  • 在详细视图中显示注释数组是如何声明和初始化的。
  • @AnnaKarenina 我有 59 个注释

标签: objective-c ios uitableview mkmapview


【解决方案1】:

cellForRowAtIndexPath 中,您声明了一个名为annotations 的新局部变量,它与您在viewDidLoad 中创建的annotations 数组无关(我假设您在其中添加注释) .

然后在cellForRowAtIndexPath,这一行:

for(Location *annotation in [(MKMapView *)self annotations])

因为self 中没有annotations 属性而失败。在viewDidLoad 中,您声明了一个名为annotations 的局部变量,但它在该方法之外不可见或不可访问。

上述行的另一个问题是您将self 转换为MKMapView *self 很可能是 UIViewController。它包含一个地图视图,但它本身不是一个。


您需要首先在详细视图的类级别声明您的 annotations 数组,以便它可用于所有方法。在详细视图.h 文件中:

@property (nonatomic, retain) NSMutableArray *annotations;

顺便说一句,我会给它取个不同的名字,这样它就不会与地图视图自己的annotations 属性混淆。

在.m中,合成它:

@synthesize annotations;

viewDidLoad 中,像这样创建它:

self.annotations = [NSMutableArray arraywithObjects...
[map addAnnotations:self.annotations];

numberOfRowsInSection方法中,返回数组的计数:

return self.annotations.count;

然后在cellForRowAtIndexPath:

if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if(indexPath.section == 0)
{
    cell.textLabel.text = [[self.annotations objectAtIndex:indexPath.row] title];
}
return cell;

【讨论】:

  • 地图和表格是否在同一个视图控制器中?在另一条评论中,您提到了详细视图。什么是“masterviewcontroller”,什么是“detail view”?
  • 每当我使用 self.annotations 时,我在“MasterViewController*”类型的对象上找不到属性“注释”的错误
  • 我在 iPad 上使用 splitview。表格视图用于masterviewcontroller,detailview用于地图
  • 首先,在masterviewcontroller的viewDidLoad中,放这个NSLog,让我知道它说了什么:NSLog(@"dvc = %@", [self.splitViewController.viewControllers objectAtIndex:1]);
  • dvc =
【解决方案2】:

您正在报告该行的崩溃(我假设)

我看到的问题是崩溃原因是

cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

annotations 数组只是在本地初始化,上面的几个语句不包含任何值..??

【讨论】:

  • 我的详细视图中有一个带有声明 NSMutableArray *annotations 的注释数组。
  • 那么你怎么能在这里说你的局部变量annotations没有在代码中被考虑,而不是你提到的那个?
  • 我无法在表格视图中填充地图注释。
  • 我不明白你在说什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多