Search code examples
ios4uitableviewios5uimenucontroller

iPhone SDK: UIMenuController to popup above selected UITableCell


I am trying to have the UIMenuControl popup above whatever cell is selected by the user....

Currently I have this code below, which always puts the UIMenuControl in the middle of the screen on an iPhone...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

[self becomeFirstResponder];

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"View" action:@selector(viewListing:)];
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editListing:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Call" action:@selector(callListing:)];
UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Open Houses" action:@selector(openHouses:)];

UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:CGRectMake(0, 205, 320, 200) inView:self.view];
menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1, menuItem2, menuItem3, nil];
menuController.arrowDirection = UIMenuControllerArrowDown;
[menuController setMenuVisible:YES animated:YES];
}

any help would great be appreciated.


Solution

  • It is clear that your menu is always at the center: you are hard-coding the coordinates.

    Solution:
    You first have to calculate the position of the cell. Then calculate your targetRect frame from that.

    Hint: you can get the frame of your cell with rectForRowAtIndexPath:.