How to call PopOver Controller from UITableViewCell.accessoryView?

11,213

Solution 1

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Changing inView:cell.accessoryView to inView:cell

Solution 2

here's for swift:

let cell = tableView.cellForRowAtIndexPath(indexPath)
let rect = tableView.convertRect(tableView.rectForRowAtIndexPath(indexPath), toView: tableView)


    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("loadZone") as! UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)

    nav.modalPresentationStyle = UIModalPresentationStyle.Popover

    var popover = nav.popoverPresentationController

    popoverContent.preferredContentSize = CGSizeMake(100,200)
    popover!.sourceView = tableView
    popover!.sourceRect = rect

    self.presentViewController(nav, animated: true, completion: nil)

enter image description here

Share:
11,213
Vic
Author by

Vic

Updated on June 04, 2022

Comments

  • Vic
    Vic about 2 years

    First I would like to say that I'm really new to ipad/ipod/iphone development, and to objective-c too.

    With that being said, I'm trying to develop a small application targeting the iPad, using Xcode and IB, basically, I have a table, for each UITableViewCell in the table, I added to the accessoryView a button that contains an image.

    Here is the code:

    UIImage *img = [UIImage imageNamed:@"myimage.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
    button.frame = frame;   // match the button's size with the image size
    
    [button setBackgroundImage:img forState:UIControlStateNormal];
    
    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    

    So far, so good, now the problem is that I want a PopOver control to appear when a user taps the button on the accessoryView of a cell.

    I tried this on the "accessoryButtonTappedForRowWithIndexPath" of the tableView:

    UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;
    
    //customViewController is the controller of the view that I want to be displayed by the PopOver controller
    customViewController = [[CustomViewController alloc]init];
    
    popOverController = [[UIPopoverController alloc]
    initWithContentViewController: customViewController];
    
    popOverController.popoverContentSize = CGSizeMake(147, 122);
    
    CGRect rect = button.frame;
    
    [popOverController presentPopoverFromRect:rect inView:cell.accessoryView
    permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    

    The problem with this code is that it shows the Popover at the top of the application View, while debugging I saw the values of "rect" and they are:

    x = 267
    y = 13
    

    so I think it is pretty obvious why the PopOver is being displayed so up on the view, so my question is, how can I get the correct values for the PopOver to appear just below the button on the accessoryView of the cell?

    Also, as you can see, I'm telling it to use the "cell.accessoryView" for the "inView:" attribute, is that okay?