开发者

iPhone MKMapView in UITableViewCell

Each cell of my UITableView has to display a small MKMapView with an annotation inside. I managed to do this by creating my custom Cell and configuring its MKMapView in the cellForRowAtIndexPath method. The locations are based on an array I build earlier.

Everything works fine but the MKMapViews refresh everytime I scroll the UITableView. Is ther a way to "cache" the MKMapViews开发者_运维问答 ?

Thanks


You could cache a UIImage of your MKMapView using the method described here: Get map image from MKMapView

You should use that UIImage as often as possible, and only display the actual map when the user needs to interact with it. For best performance, you could try always using the UIImage in the tableview and when the user taps on the row, load the real map (perhaps in its own view controller).


You probably shouldn't be configuring the map views in cellForRowAtIndexPath, instead configure them and store them in an array in viewDidLoad or awakeFromNib. Then just set the cell's map view in cellForRowAtIndexPath.


Just create a uitableview cell global object in the header file with @property and assign the cell to the global object, if the cell is nil.

Then each time you enters the cellforrowatindexpath check whether the object is nil or not.. If not nil , return the global object .

//in class.h

@property (nonatomic, retain) UITableViewCell *mapCell;

//in cellForRowAtIndexPath

if (mapCell == nil) {
                        allocate the cell
                    }
                    else{
                        return mapCell;
                    }


There is a better way, use MKMapSnapshotter to create images from the map (in the background thead - no lag!), add the pins manually and then you can cache the images and make it blazing fast. I've provided some code here - http://synappse.co/mkmapview-too-slow-use-preview/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜