Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the frame of a table view cell in the coordinate space of the table view?

I have a table view with a bunch of cells in it. I want to get the frame of a cell, not in its superview's coordinate space, but in the table view's coordinate space.

A picture would probably explain this better

enter image description here

See the red rectangle? That is the frame of the table cell. And the blue rectangle is the frame of the table view. I want to get the red rectangle that is relative to the blue rectangle.

I tried to use the frame property of UITableViewCell. But that doesn't work because it returns (0, 0, 320, 44). The Y value shouldn't be zero because obviously the cell is not at the top of the screen.

According to the view hierarchy, this is the intended behaviour, because the superview of a table cell is apparently this UITableViewWrapperView, not the table view:

enter image description here

while the table view is this:

enter image description here

So is there a method/property that can get me a CGRect that represents the frame of the table view cell in the coordinate space of the table view?

I think there is a function that can convert a CGRect in a coordinate space to another. But I forgot its name and how to use it. I don't know whether it is helpful in this situation.

like image 319
Sweeper Avatar asked Jun 11 '16 12:06

Sweeper


1 Answers

Please use below code

let rectOfCell= tableView.rectForRowAtIndexPath(indexPath)
let rectOfCellInSuperview = tableView.convertRect(rectOfCell, toView: tableView.superview)
like image 135
Jasmeet Kaur Avatar answered Nov 04 '22 08:11

Jasmeet Kaur