1

我正在尝试使用我的注释数组填充我的表格视图,但 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];
4

2 回答 2

2

In cellForRowAtIndexPath, you are declaring a new, local variable named annotations which has nothing to do with the annotations array you are creating in viewDidLoad (I assume that's where you're adding the annotations).

Then in cellForRowAtIndexPath, this line:

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

fails because there is no annotations property in self. In viewDidLoad, you declared a local variable named annotations but it is not visible or accessible outside that method.

The other issue with the above line is that you're casting self as an MKMapView *. Most likely self is a UIViewController. It contains a map view but is not itself one.


You need to first declare your annotations array at the detail view's class level so it's available across all methods. In the detail view .h file:

@property (nonatomic, retain) NSMutableArray *annotations;

By the way, I'd name it something different so it's not confused with the map view's own annotations property.

In the .m, synthesize it:

@synthesize annotations;

In viewDidLoad, create it like this:

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

In the numberOfRowsInSection method, return the array's count:

return self.annotations.count;

Then in 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;
于 2012-08-16T13:55:36.903 回答
0

您正在报告那条线上的崩溃(我假设)

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

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

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

于 2012-08-16T12:36:42.183 回答