How to get rect size of UITableViewCell?

11,538

Had to do this recently. You need to use [tableView rectForRowAtIndexPath].

However, one of the things that can catch you out is that as you scroll through the UITableView you need to take account of the offset (ie something reporting its y position as 3000 pixels is of course meaning 3000 from the top of the UITableView - not 3000 pixels on the screen).

In addition to this, it will naturally appear at the top of the cell, so you need to add half the height of the cell to the calculations.

This code worked well for me:

CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
CGPoint yOffset = self.tableView.contentOffset;
[self.popoverController presentPopoverFromRect:CGRectMake(frame.origin.x, (frame.origin.y + 45 - yOffset.y), frame.size.width, frame.size.height)

In my case each cell was 90 pixels high, hence the '+45' bit. Substitute half your own cell height.

Share:
11,538
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on June 20, 2022

Comments

  • Sheehan Alam
    Sheehan Alam almost 2 years

    How can I get the rect size of a UITableViewCell?

    I am trying to display a pop hover every time the user clicks on a cell, and would like the pop hover to appear centered on every cell:

    [replyPopover presentPopoverFromRect:CGRectMake(77, 25, 408, 68) 
                                  inView:self.view 
                permittedArrowDirections:UIPopoverArrowDirectionDown 
                                animated:YES];
    
  • Veeru
    Veeru about 12 years
    That was very helpful. I was rubbing the wrong one, i was cracking my head wiht [[tableView cellForRowAtIndexPath:indexPath] height] - Your solution was what i actually need. Thanks a bunch!