Faser
Faser

Reputation: 1274

Popover from tableViewCell positioning issue

I have a popover which is presented from another popover. They both have tableViews. The second popover for the first row is fine, the arrow is pointing to the middle of the cell. But the others point to the top of the cell, and the arrow is at the top of the popover, not in the middle …

Thats how i construct the popover

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

self.editController = [[TitleViewController alloc] init];
[self.editController setSizeWithWidth:self.view.bounds.size.width AndHeight:140.0];
self.editController.directoryString = [directoryPath stringByAppendingPathComponent:[self.tableView cellForRowAtIndexPath:indexPath].textLabel.text];

self.editPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.editController];



CGRect aFrame = [self.tableView rectForRowAtIndexPath:indexPath];

[self.editPopoverController presentPopoverFromRect:aFrame inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

} Here is the misplaced second popover

EDIT

IF I change Arrow Direction to Any, it looks like this.

Back again to ArrowDirection = Left

If I make the changes that were recommended (note that aFrame.size.height * 2and not divided by two), the arrow points to the middle of the cell, but still is awkwardly positioned in the new popover frame (not in the middle)

enter image description here

Upvotes: 3

Views: 2313

Answers (4)

Totumus Maximus
Totumus Maximus

Reputation: 7573

I actually encountered somewhat the same problem, when trying to expand a cell to full tableView-size. Asking a cell frame and use it outside the tableview isn't as easy as I thought it was. Yet I found the solution after some direct questions at mr. Google.

This first method will actually give the frame of the cell from perspective of the tableView.

CGRect rectInTableView = [myTableView rectForRowAtIndexPath:someIndexPath];

This is useful but not the complete story (at least is wasn't for me). You might want to translate that rect to the View were it actually matter to have that rect.

This second method will translate this rect to the perspective of the view where you want the popover to be. In my case [myTableView superView] but it can be any visible view.

CGRect rectInSuperview = [myTableView convertRect:rectInTableView toView:someView];

This rectInSuperView is probably what you seek. It solved a lot of my cellFrame issues and it might as well help for you.

The arrow will always point at the middle of a rect so if your rect isn't working fine "hacking" the rect to make it fit your needs isn't the best way to solve this problem.

I hope this solution works for you too. And if not feel free to leave a comment and I'll see if I can help you further.

Upvotes: 4

Akki
Akki

Reputation: 1477

You need to determine, what the row calls to popover. When you did it, try to replace CGRect aFrame = [self.tableView rectForRowAtIndexPath:indexPath]; with

CGRect aFrame = [self.tableView rectForRowAtIndexPath:indexPath];

edit: fixed the code to reflect the actual solution

Upvotes: 1

bmeulmeester
bmeulmeester

Reputation: 1117

I think there are 2 possible solutions to this problem:

  1. Change the height value of aFrame to aFrame.size.height / 2
  2. Change the presentPopoverFromRect:inView:permittedArrowDirections:animated:, and make sure the TableViewCell's contentView is set as the 'inView' parameter.

Hope this helps.

Upvotes: 2

magtak
magtak

Reputation: 1006

can't see why it's happening but you may as well add (cell.frame.height/2) to the popover's frame.y and fix it :)

Upvotes: 1

Related Questions