3

This was a different question but what I thought was wrong was not wrong so I adjusted the title to reflect what I really figured out.

I am working on an animation that takes a copy of an image in the cell.imageView of a normal UITableViewCell and moves it to the tab bar on the bottom of screen, to simulate putting an item in a cart. I can copy the image and place the copy as a subview of the window, but I can't figure out how to get the absolute position of the original image so I can place it on top as the starting point of my animation.

The image always shows up at the top of the screen as if placed at 0,0.

Sorry for the noobish nature of the question, I'm sure i'm missing something very obvious.

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
UIImageView *originalImgView = cell.imageView;
UIImageView *img = [[[UIImageView alloc] initWithImage: cell.imageView.image] autorelease];

NSLog(@"Original %@", originalImgView.bounds);  // Shows "Original (null)" at runtime.
NSLog(@"Window Points %@", [self.view.window convertPoint:originalImgView.frame.origin toView:nil]);
NSLog(@"Cell Points: %@", cell.bounds);  // Shows "Original (null)" at runtime.

[img setFrame:CGRectMake( originalImgView.bounds.origin.x, originalImgView.bounds.origin.y ,
        cell.imageView.image.size.width, cell.imageView.image.size.height)];

[self.view.window addSubview:img];

NSLog(@"Selected Cell: %@", cell); // Shows cell info at run time, works

}

Donk
  • 81
  • 6
  • I think I figured out what's up. When I call [self tableView:tableView cellForRotAtIndexPath....]. I am not getting a reference to the existing cell. I am getting a new "copy" of the cell from scratch. So when I try to get information about the position of the cell I am actually getting the location of my cell copy which has not been layed out properly. – Donk Feb 25 '11 at 01:31

2 Answers2

5

So I was doing this all wrong. I was making a new cell with the same content instead of using the current cell. I fixed this by adding a tag to the cell when it was created.

cell.tag = [indexPath row];

Except that gave me a problem when [indexPath row] == 0. So I added 1000 to it and that problem went away.

cell.tag = (1000 +[indexPath row]);

Then I just replaced this line

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

With

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:(1000 + [indexPath row])];

Now I got a reference to the actual cell that is already in the table view and I am getting proper frame values.

Donk
  • 81
  • 6
2

With more than 1 section this might provide more unique tags

([indexPath section]+1) avoids multiplying by section 0 (1000 * ([indexPath section]+1)) gives steps of 1000, 2000, etc.

In cellForRowAtIndexPath set tag... cell.tag = ((1000 * ([indexPath section]+1)) +[indexPath row]);

In didSelectRowAtIndexPath retrieve it... UITableViewCell *selectedCell = (UITableViewCell *) [playlistTableView viewWithTag:((1000 * ([indexPath section]+1)) + [indexPath row])];

assuming your list doesn't go over 1000 cells in each section.

This was a great question with a very useful answer by Donk, thanks a lot for posting the details.

macasas
  • 336
  • 2
  • 16
  • I like Donk's idea of using view tags. This will only work for visible cells, of course, since they're recycled. But that's fine when responding to UI events! A quick search for [mapping two integers onto a unique value](http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way) turns up _Szudzik's function_: `a >= b ? a * a + a + b : a + b * b;` where a, b >= 0. Pop in your section and row to get a unique view ID for your cell. – Nuthatch Jan 04 '14 at 03:10