Question

In Xcode I have created a UITableView with Dynamic Prototypes. The first cell is composed of a button, a label, and an MKMapView.

In my ...cellForRowAtIndexPath: method I use the following code:

UITableViewCell *header = [self getNewCell:@"entity_header"];

// Send map to back (put map behind everything else)
MKMapView *MapView = (MKMapView*)[header viewWithTag:2];
[tableView sendSubviewToBack:MapView];
// Also tried using `header` here. [header sendSubviewToBack:MapView];

// Set the image of the back button.
UIButton *backButton = (UIButton*)[header viewWithTag:1];
UIImage *backBtnImg = [UIImage imageNamed:@"images/backBtn.png"];
[backButton setImage:backBtnImg forState:UIControlStateNormal];
[tableView bringSubviewToFront:backButton];
// Also tried using `header` here. [header bringSubviewToFront:backButton];

I would like to send the MKMapView behind the other components however the code above does not work as intended.

NOTE: The reason I use viewWithTag here is so that I don't have to create outlets for all of the components. (It's tricky creating outlets from inside a UITableViewCell without using more implementation files)

Was it helpful?

Solution

I think you're using custom cell. So problem may be in this line. MKMapView *MapView = (MKMapView*)[header viewWithTag:2]; Instead try this.

MKMapView *MapView = (MKMapView*)[header.contentView viewWithTag:2];

And they go with this .

[header.contentView sendSubviewToBack:MapView];

Note: If all content's subview have bounds less than MKMapView means, MKMapView always will be in screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top