1

I am trying populate my table view using my array of annotations but XCode seems to give me a breakpoint any time I add this code.

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;

My annotations:

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

2 Answers2

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;
  • Are the map and table in the same view controller? In another comment you mention a detail view. What's the "masterviewcontroller" and what's the "detail view"? –  Aug 16 '12 at 14:34
  • the error i receive Property 'annotations' not found on object of type 'MasterViewController*' any time i use self.annotations – James Frempong Aug 16 '12 at 14:39
  • Am using splitview for iPad. The table view is for the masterviewcontroller and detailview for the map – James Frempong Aug 16 '12 at 14:40
  • First, in the viewDidLoad of the masterviewcontroller, put this NSLog and let me know what it says: `NSLog(@"dvc = %@", [self.splitViewController.viewControllers objectAtIndex:1]);` –  Aug 16 '12 at 15:13
  • Next: In masterviewcontroller, do you already have a reference to the detailviewcontroller and are you using it? –  Aug 16 '12 at 15:38
  • if i populate the table manually with the same string names is there anyway to connect them using the string names. – James Frempong Aug 16 '12 at 15:49
  • No, the basic idea is this: The two view controllers need to share data. My suggestion would be to let the masterviewcontroller create and manage the annotations array and have the detailviewcontroller ask the masterviewcontroller for the data. Other possibilities (which I don't recommend) are to put the data in a singleton object or to put the array in the app delegate. –  Aug 16 '12 at 15:57
  • I recommend this approach: http://stackoverflow.com/questions/8031238/uisplitviewcontroller-master-detail-communication –  Aug 16 '12 at 16:04
0

You are reporting a crash (i assume) on that line

the problem that I see is crash reason would be

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

annotations array is just initialized locally few statements above which does not hold any values..??

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41