Access Existing UITableViewCell

It was a different question, but what I thought was wrong was wrong, so I adjusted the title to reflect what I really understood.

I am working on an animation that takes a copy of the image in cell.imageView of a regular UITableViewCell and moves it to the tab bar at the bottom of the screen to simulate putting an item in the trash. I can copy the image and place the copy as a subspecies of the window, but I cannot 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 is always displayed at the top of the screen, as if it were placed at 0.0.

Sorry for the noobic nature of the question, I'm sure I missed something very obvious.

- (Invalid) Tableview: (UITableView *) tableView accessoriesButtonTappedForRowWithIndexPath: (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

}

+3
source share
2 answers

So I did it all wrong. I was making a new cell with the same contents 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 I had a problem when [indexPath row] == 0. Therefore, I added 1000 to it and this problem disappeared.

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])];

, , .

+5

1

([indexPath section] +1) 0 (1000 * ([indexPath section] +1)) 1000, 2000 ..

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

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

, 1000 .

, .

+2

Source: https://habr.com/ru/post/1794582/


All Articles