Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the exact location of a UITableViewCell

Given a UITableView, how can I find the location of a specific UITableViewCell? In other words, I want to get its frame relative to my iPhone screen, not relative to the UITableView. So if my UITableView is scrolled up, the location of each UITableViewCell should be higher on the screen, etc.

like image 372
CodeGuy Avatar asked Jun 08 '11 14:06

CodeGuy


2 Answers

You could also use the rectForRowAtIndexPath method to get the location of a UITableView by sending the indexPath for that.

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath 

So use as below:

CGRect myRect = [tableView rectForRowAtIndexPath:indexPath]; 
like image 83
Jhaliya - Praveen Sharma Avatar answered Oct 10 '22 12:10

Jhaliya - Praveen Sharma


Apart from rectForRowAtIndexPath you need to consider the scrolling.

Try this code:

 // Get the cell rect and adjust it to consider scroll offset  CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];  cellRect = CGRectOffset(cellRect, -tableView.contentOffset.x, -tableView.contentOffset.y); 
like image 29
Tomasz Avatar answered Oct 10 '22 14:10

Tomasz