我正在尝试使用我的注释数组填充我的 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/