views:

1184

answers:

1

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?

+1  A: 

Try using button.bounds instead of button.frame since the rect is relative to the inView.

Also note that if the cell is near the bottom of the screen, the popover may not appear at the right size because you are forcing the arrow in the Up direction. You should either handle this manually or just use Any.

DyingCactus
many thanks, that totally worked!
Vic