MKMapView blocks main thread while used in UITableView

414 views Asked by At

I have a basic UITableView that contains 20 cells, one of the cells is a MKMapView just dropped in as is without any custom code (not even setRegion:Animated), if I open the view at the first time and scroll down the table view (towards the map's cell), there is a noticeable hang happens for the app, and if I use setRegion:Animated (without dropping any pin) the hang gets longer. However, this block of the main thread disappears on later attempts to scroll towards the map's cell.

I can't use the MKSnapShotter because I want the user to interact with the map so an image won't satisfy the case.

the table view does not make any block on the main thread if the map does not exist.

how to avoid blocking the main thread while showing the map's cell for the first time ?

1

There are 1 answers

0
Manav Gabhawala On

First make a tableView IB outlet if you haven't already done so. Create the cell in view did load using tableView.dequeueReusableCellWithIdentifier("mapCellIdentifier") and store that in an ivar. Then in the data source return the cell when the index path is the one you want so inside of cellForRowAtIndexPath do

if (indexPath.row == wantedRow) { 
    return mapCell 
}

The mapCellIdentifier needs to be set in the cell you created in interface builder for the map. This is assuming you have dynamic cells.