1

I programmatically set up a popover, and try to present it in (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

with the following code:

popoverView.popoverContentSize = CGSizeMake(580, 760);
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[popoverView presentPopoverFromRect:cell.bounds inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

This works fine, however, it ignores the size, so that the view gets cut off, because the cell is near the middle of the screen. When I change it to (UIPopoverArrowDirectionRight|UIPopoverArrowDirectionLeft), it also ignores this and displays the popover up or down.

What I want is for the popover to be displayed closer to the left side of the cell, so that it has space to open to the side, but I get a lot of errors with the tableview whenever I attempt to edit the rect it is displayed from... I have tried many different ways, for example:

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect frame = CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, cell.bounds.size.width/4, cell.bounds.size.height);
[popoverView presentPopoverFromRect:frame inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

and no matter what I do, unless I use the first code (FromRect: cell.bounds inView:tableView) I get the two following errors:

Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:8357*

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource is not set'*

But the tableview datasource is definitely set, and I only get these errors when I tried to change the rect the popover is presented from. I have also tried to manually calculate the position of the cell to get the frame instead of using the cell, aka by multiplying the height of cells by indexPath.row for the y value...

Any ideas as to why I'm getting these errors?? Or how I can display the full popover properly from the tableview?

Edit: Also, there's no issues with me getting the cell and creating a frame based on the cell's bounds.. The line where the errors come from is always on the "presentPopoverFromRect:" line.

Solution: I was finally able to achieve the desired affect of having the popover not from the center of the cell with a CGRectOffset without any errors:

CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:tableView];
[popoverView presentPopoverFromRect:CGRectOffset(rect, -400, 0) inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

Another Edit: The really weird thing is that that code works for the first section I tested it in. But my tableview has 2 sections, and for some reason the popover is working fine in the second section, but I'm still getting the errors in the first section? Any ideas about the errors??

2 Answers2

0

Use tableview's rectForRowAtIndexPath method for getting rect

Try this,

         CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath]
                                      toView:tableView];

        [popoverController presentPopoverFromRect:rect
                                           inView:tableView
                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                         animated:YES];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • This worked when I didn't mess with the rect, but when I tried to edit it I got those same two errors. I just added: " CGRect frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/4, rect.size.height); " in between. – HelloThisIsMyName Jul 30 '14 at 05:42
0

Your code sample isn't quite right:

[popoverView presentPopoverFromRect:cell.bounds inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

Keep in mind that a the origin of cell.bounds is (0, 0), i.e. the bounds rect is relative to the cell's coordinate system. So, the inView argument should be cell:

[popoverView presentPopoverFromRect:cell.bounds inView:cell permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

Alternatively, you can specify the cell's rect in the table view's coordinate system using cell.frame:

[popoverView presentPopoverFromRect:cell.frame inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

Hope that gets you on the right path.

UPDATE

Also, the same error was reported and solved in this question.

Community
  • 1
  • 1
Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
  • I see what you're saying... I actually just mixed frame/bounds up because I had been trying every way of doing it that I could think of. But the problem is that I want to edit cell.bounds or cell.frame so that the center of the frame is off to one side, and it's when I use an edited rect that I get errors. Any of these do get me a popover from the center of my cell, but I need it to the side to fit the full view. – HelloThisIsMyName Jul 30 '14 at 06:13
  • And the question that you linked to doesn't help me because the tableview is set up through storyboard with autolayout, and the response to that question is about when to add the subviews? I'm not sure how to apply that here? – HelloThisIsMyName Jul 30 '14 at 23:42
  • Don't know. You've only provided 3 lines of code, which isn't much to go on. – Timothy Moose Jul 31 '14 at 01:08
  • Well I posted the only lines of code which affect the error, which always appears on the presentPopoverFromRect line.. – HelloThisIsMyName Jul 31 '14 at 17:58