Nate
Nate

Reputation: 7856

Duplicate a UITableViewCell - iPhone

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

Upvotes: 0

Views: 1477

Answers (2)

Vladimir
Vladimir

Reputation: 7801

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.

Upvotes: 3

David Gelhar
David Gelhar

Reputation: 27900

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

Upvotes: 0

Related Questions