0
votes

I created a custom tableCell which is derived from TableViewCell. It only contains on property which is a MkMapView.

In the ViewDidLoad I did this.

`- (void)viewDidLoad { [super viewDidLoad];

//Accessing mapView

MapViewTableCell *mapViewCell = [[self tableView] dequeueReusableCellWithIdentifier:@"MapViewCell"];
self.mapView = mapViewCell.mapView;
self.mapView.delegate = self;

}`

Under this tableViewCell I have this @property (nonatomic, strong) MKMapView *mapView;

So in the rest of the code I am accessing the mapView through the same way I was accessing it which is self.mapView.

But after that I just find that although through NSLog I cannot see anything wrong with my self.mapView object, its setRegion simply doesn't work and I could not update any annotation on the mapView any more.

Has anybody find such issue before?

1

1 Answers

1
votes

When you dequeue a cell outside of cellForRowAtIndexPath, as you're doing in your viewDidLoad method, that cell won't get every reused because the table view isn't managing it's life-cycle. Therefore, in your cellForRowAtIndexPath, you're getting a new "MapViewCell" instance.

Assuming you will only ever have one map view in your table, your best bet is to do one of the following:

  1. Instantiate the map view in your view controller and in cellForRowAtIndexPath, add it to the cell.
  2. Instead of dequeuing the map view cell, just instantiate one that you keep a reference to in your view controller and in your cellForRowAtIndexPath, always return that cell.