How to create a UITableViewCell with “copy” ability
by Hans Pinckaers
It’s possible to copy the content of a UITableViewCell in the Contacts app of Apple. When you tap and hold you will see a small menu with “Copy”. I needed this functionality myself, so I subclassed the class. You are free to use this in any application, but it would be awesome if you mention me in the credits of your App.
It’s easy to use
- Copy the files in your project.
- Use this code to create a cell:
cell = [[[HPCopyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
Download
This is perfect! Thank you so much for providing this. It is awesome you provide this type of help. I was beating my head against a wall on this one. Works great.
Kevin
@finalpointlogic
[...] Check out his site for the files and more. http://www.hanspinckaers.com [...]
Excellent – was missing the cancelled touches piece. Thank you!
Great stuff, thanks!
[...] UITableViewCell with “copy” ability [...]
Great code, thank you!!!
It’s actually quite a bit simpler now. Just implement the following three methods in the UITableView controller:
-(void)tableView:(UITableView*)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender{ UIPasteboard *gpBoard = [UIPasteboard generalPasteboard]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.detailTextLabel.text && ![cell.detailTextLabel.text isEqual:@""]) { [gpBoard setString:cell.detailTextLabel.text]; } else { [gpBoard setString:cell.textLabel.text]; } [cell release];}
-(BOOL)tableView:(UITableView*)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender{ if (action == @selector(copy:)) return YES; else return [super canPerformAction:action withSender:sender];}
-(BOOL)tableView:(UITableView*)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath*)indexPath{ return YES;}
Thank you for your perfect code.
Jared’s code is also good, but should remove [cell release]
because it makes error.
Also, Jared’s code is not documented one by Apple and someday could be deprecated or not supported, I’m afraid of that.
But HP’s code is perfect using only documented ones.
Thanks again, Hans, I’ve just followed you @ Twitter.
Awesome! Thanks for the help!
Thank you, I can’t believe this is not built in ! You saved me a lot of time.