views:

209

answers:

2

Hi everyone, I would like to create an effect to a cell of a UITableView. The effect is: duplicate the cell and move the duplicated cell (the original stays at its place). My problem is to duplicate the cell...

I've tried:

Code:

UITableViewCell *animatedCell = [[UITableViewCell alloc] init];
animatedCell = [[self cellForRowAtIndexPath:indexPath] copy];

but UIView doesn't seem to implement the copy... How can I do it?

Thanks

A: 

You generally don't want to manually allocate cells (except in your tableView:tableView cellForRowAtIndexPath: delegate method). Instead, make whatever changes you need in your data model to reflect the new cell, then tell the tableview to insert a new row:

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
David Gelhar
I don't want to modify my table view... I just want to copy one cell from my tableview and use it to make an animation!
ncohen
A: 

If you need only duplicated image of your cell for animation (not a real view with all subviews) you can simply copy cell image:

UITableViewCell * aCell = [tableView cellForRowAtIndexPath:indexPath];
UIGraphicsBeginImageContext(aCell.frame.size);
[aCell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *aCellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * imageView = [[UIImageView alloc] initWithImage:aCellImage];

Or if you need real view create it. And take into account that UITableView doesn't create cells, you do it in tableView: cellForRowAtIndexPath: method of your tableView dataSource. So use same code to create another cell with specified indexPath... But do not create UITableViewCell object, create UIView instead and add it to containView of cell. And when you need duplicate cell create another instance of same UIView and use it in your animation.

Vladimir
PERFECT!!! thanks so much...
ncohen