views:

1416

answers:

3

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!

+4  A: 

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.

Corey Floyd
I got so excited when I saw this. However the coordinates returned are the same as those for self.frame. (I have subclassed UITableViewCell, and my UIButton's touch method is within the subclass.)The above was a drop-in replacement for self.frame. Here is an example of values:Clicking on a cell in the first "page" of the table:0,158,32,480clicking on a cell further down:0,222,32,480Scrolling the table to reveal cells below, and then clicking on one:0,2238,32,480Any other thoughts?
mahboudz
I updated the answer to take that in to account
Corey Floyd
Thank you. That's it! Here is the way I used it: CGRect cellFrame = [self.superview convertRect:self.frame toView:tableViewController.view];
mahboudz
+1  A: 

I was trying to do something very similar. On the iPad, when a table cell is selected, I wanted to show a popup next to the cell. The following code works for doing such a thing. In this case, self is a UITableViewController.

CGRect rect = [cell.superview convertRect:cell.frame toView:self.view];
logancautrell
A: 

thanks logancautrell...

i want to do same which u have done you but i m not getting true rectangle of cell bacause i have scrollable table with many rows.please help.....

piyush