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

14,716

Please use below code

let rectOfCell= tableView.rectForRowAtIndexPath(indexPath)
let rectOfCellInSuperview = tableView.convertRect(rectOfCell, toView: tableView.superview)
Share:
14,716

Related videos on Youtube

Sweeper
Author by

Sweeper

I specialise in four programming languages: C#, Java, Kotlin and Swift. I don't know what to say here, so let me show you some of my apps, as well as my blog. Also you can donate to me if you'd like :D iOS Apps: Pocket Cheque Helper CusTimer Pushy Squares Math Toolbox Free Color Bible EZDiary TZoney Directional Snake LongLatMap Not Really Color Blind? 城迹单

Updated on September 15, 2022

Comments

  • Sweeper
    Sweeper over 1 year

    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.

  • Raunak
    Raunak about 5 years
    tableView.rectForRow(at: indexPath)