17

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

1
  • me too, I met the same issue, even on UI the frame has width and height, but result returned 0, 0, 0, 0 for x, y, width, height.
    – RainCast
    Commented Apr 18, 2016 at 21:56

4 Answers 4

66

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)
2
  • What's the equivalent for UICollectionView?
    – Kyle Clegg
    Commented Aug 16, 2013 at 6:51
  • @Zigglzworth: Why? Did you debug and check the values? Commented Dec 17, 2013 at 13:46
3

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.

2

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.

0

I suspect the cells are held within 'cell sized' subViews of the UITableView so you are seeing a frame relative to that view.

I don't have an actual an answer for you but, I would suggest checking out UIScrollView's delegate class: UIScrollViewDelegate. It responds to – scrollViewDidScroll: and you can manually work out your offset from that. UIScrollView is a superclass of UITableView.

You can convert points (such as your origin) to another view's co-ordinates using UIView's - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view method.

Not the answer you're looking for? Browse other questions tagged or ask your own question.