objective-c - 注释和表格 View

标签 objective-c ios uitableview mkmapview

我正在尝试使用我的注释数组填充我的 TableView ,但每当我添加此代码时,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];

最佳答案

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

然后在 cellForRowAtIndexPath 中,这一行:

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

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

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


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

@property (nonatomic, retain) NSMutableArray *annotations;

顺便说一下,我会给它取个不同的名字,这样它就不会与 map View 自己的 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;

关于objective-c - 注释和表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11986820/

相关文章:

iOS UILocalNotification 已计划,但未触发

c++ - 如何将使用 tchar.h 处理 unicode 的 Windows C++ 移植到 iOS 应用程序

ios - 如何在 iOS 中使用 React Native 保存下载的文件并在文件应用程序中查看?

ios - 创建应用时出现 "Static table views are only valid when embedded in UITableViewController instances"如何解决?

swift - 使用 SDWebimage 下载并缓存 MKMapSnapshotter 图像

iphone - IOS:带有自定义 uitableviewcell 的动态高度

ios - 仅以英文获取 CLGeocoder 值

c++ - 单元测试中的失败默认参数

ios - 从 UIActionSheet 呈现 UIPopover

objective-c - 'MySwiftClass' 没有可见的@interface 声明选择器 'addX:andY'