Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine coordinates of a UITableViewCell while scrolling

My goal is to have the UITableViewCells fade in/out when they are approaching the bounds of the UITableView and about to be covered/revealed.

The approach I have been trying is to get the coordinates of the UITableViewCell during a scroll event. The problem is that every cell seems to be at 0,0. I have tried converting the coordinates to the parent table and view, but they still come out at 0,0.

So in general, if anyone knows a way to get the coordinates, or of a better way to go about fading UITableViewCells in and out based on their position, I would greatly appreciate any advice you may have.

Thanks for your time, Joel

like image 934
Joel Avatar asked Mar 26 '09 22:03

Joel


3 Answers

The first step is to use

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];

which will report the CGRect of a cell within the tableView. However, this value does not change as the tableView scrolls. It is the position relative to the first row of the table (and not the first visible row). To get the position of the cell on the screen you have to convert it to the superviews coordinate system using

CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];

So the following line does it all

CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];

Swift 4

// Consider the indexPath at row 1, section 0.
let rectWithinTableView : CGRect = self.tableView.rectForRow(at: IndexPath(row: 1, section: 0))
let rectWithSuperViewCoordinates : CGRect = self.convert(rect: rectWithinTableView, to: self.tableView.superview)
like image 102
Christian Fries Avatar answered Nov 15 '22 18:11

Christian Fries


Why not an overlay with a partially transparent gradient PNG in a UIImageView that's less translucent at the top and bottom?

Messing with cell drawing in table scrolling is going to take a big performance hit.

like image 28
Kendall Helmstetter Gelner Avatar answered Nov 15 '22 18:11

Kendall Helmstetter Gelner


You can call

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

to get the rect of any given cell. This will contain it's coordinates in the origin struct within the rect.

like image 2
Jasarien Avatar answered Nov 15 '22 19:11

Jasarien