Determine coordinates of a UITableViewCell while scrolling

两盒软妹~` 提交于 2019-11-27 12:11:15
Christian Fries

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)

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!