2
votes

Formerly, I had an MKMapView on a ViewController that would show a Circle Overlay at a given location (provided by an object that is a property of the ViewController). I refactored the VC, and now I've put the code for this map within cellForRowAtIndexPath (basically replacing "self.mapView" in viewDidLoad with "cell.mapView" in cellForRowAtIndexPath).

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

case 1: {

    static NSString *cellID = @"Cell2";
    mapTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil){
        cell = [[mapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    //    ITEM ON MAP
    double lat = [self.item.locationData[@"latitude"] doubleValue];
    double lng = [self.item.locationData[@"longitude"] doubleValue];
    cell.mapView.delegate = self;
    CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(lat, lng);
    itemLocation = cord;
    MKCoordinateRegion startRegion = MKCoordinateRegionMakeWithDistance(cord, 1500, 1500);
    [cell.mapView setRegion:startRegion animated:YES];
    [cell.mapView addOverlay:[MKCircle circleWithCenterCoordinate:cord radius:200]];

    return cell;

}
    return cell;

}

With this code above, I just get a wide-shot of the entire United States, with no overlay.

I'm not trying to display the user's current location (although I can get that to work simply by ticking the checkbox to do so in Interface Builder). Instead, I am passing in a set of coordinates that are stored with an object ("self.item") that is saved on a Parse backend.

I can access all the rest of the object's properties, and this code worked before (outside of a tableView as mentioned above), so Im thinking this has something to do with the tableViewCell itself. Is there something wrong with putting a MapView on a TableViewCell?

1
Is cell.mapView an IBOutlet? If yes, make sure it is connected to the MKMapView in the xib. In any case, make sure cell.mapView is not nil. - user467105

1 Answers

2
votes

Try this, Add the delegate in the custom cell rather than in the view controller

@interface MapTableViewCell : UITableViewCell
<MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

And set the delegate inside the cellForRowAtIndexPath to the cell itself.

// In cellForRowAtIndexPath set the delegate like:
cell.mapView.delegate = cell;