joslinm
joslinm

Reputation: 8105

How do I get the relative screen coordinates of a cell or any subview?

I have a long list of table view cells. When I attempt to get one of the frames down the list, I get something like: (0, 508) (56, 320) (x,y)(height, width)

I want to know these coordinates in regards to the current screen -- in the 320x480 range. I've tried doing things like 508 % 480 after getting the coordinates in terms of the self.view.window(via convertRange) but I'm still not seeing the results I'm expecting.

I initialize a view on top of the cell and then stretch out to fill the whole screen. This is no problem when the cell is at the top of the screen, but it results in a bad animation when the cell is lower down in the list.

Upvotes: 3

Views: 1894

Answers (1)

Filip Radelic
Filip Radelic

Reputation: 26683

Since UITableView is a subclass of UIScrollView, you can use contentOffset property to get how many pixels are from top of the table view to top of displayed content, then subtract that from cell's y position.

CGFloat relativeY = someCellFrame.origin.y - someTableView.contentOffset.y;

Upvotes: 8

Related Questions