I have a UIButton in a UITableView cell, in a UITableView. The result is a list with buttons in it. When the UIButton is pressed, it calls a touchUpInside method, and all is great. What I am having problems with is that I want to zoom out a new view from the location (frame) of the button when it is pressed. If I just get the bounds of the UIButton
origButton.frame
it is in the coordinates of the UITableCell, something like 3,50,40,25. This is the same for all buttons in the list, so no matter which button in the list gets pressed, then I get these coordinates. Now, if I actually grab the coordinates of my view
self.frame
when the touchUpInside method is called, I get something more usable. I then get the coordinates of my cell. Great!! So I can zoom out a new view from the UIButton. Great!!
But here's the problem: If you scroll up the list, tap the button, the view zooms out not from the button but from a location below the screen. The problem is, that the coordinates I have are for the cell, BEFORE it is scrolled up. In fact all coordinates for all cells reflect their actual locations in the table, with only the coordinates of the first visible set usable. Scrolling has not changed these coordinates.
I thought maybe I could use the super view's coordinates, but those are of no help.
Anyone know how to get accurate coordinates for buttons (or views or any other items) listed in a table, or is it not possible?
Here is how I calculate the center point of my new view:
floatingView.center = CGPointMake(origButton.frame.origin.x +
origButton.frame.size.width / 2,
self.frame.origin.y + self.superview.frame.origin.y);
Thanks!
Update:
Remember the a UITableView is a scrollView. So it has contentView embedded within the tableView (a scrollView).
The frame you are receiving is correct, but it is not in reference to the view frame you want. Instead the frame is in reference to the contentView. As you scroll, the cells do not change in reference to contentView.frame. However, contentView.frame DOES change in reference to the frame of the tableView as you scroll (by the amount of the contentOffset).
So this leaves 2 options:
Convert the frame of the cell from reference of the contentView to the tableView using one of the following:
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view
Or, manually subtract off the contentOffset from the frame.
I recommend the first method.
Original Answer:
I'm not sure if you subclassed UITableViewCell or are performing this code from the UITableViewController.
However, I think the method you want is:
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
This is a method of UItableView that will get you the rect for any cell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With