How to call PopOver Controller from UITableViewCell.accessoryView?

Posted by Vic on Stack Overflow See other posts from Stack Overflow or by Vic
Published on 2010-05-12T22:53:45Z Indexed on 2010/05/13 2:34 UTC
Read the original article Hit count: 583

Hi,

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?

© Stack Overflow or respective owner

Related posts about ipad-sdk

Related posts about uipopovercontroller