48
 NSString *copyStringverse = [[NSString alloc] initWithFormat:@"%@",[textview.text]];
 UIPasteboard *pb = [UIPasteboard generalPasteboard];
 [pb setString:copyStringverse];

I'm using above code for copying contents in textview, but I want to copy contents in a cell of the table. How to do this?

Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
stackiphone
  • 1,245
  • 3
  • 19
  • 41
  • 2
    This gave me the perfect solution to copy/pasting from my app - regardless of the fact that you needed something slightly different. cheers! – Dan Hanly Mar 06 '13 at 14:23

5 Answers5

72

Well you don't say exactly how you have your table view cell set up, but if it's just text inside your table view it could be as easy as:

// provided you actually have your table view cell
NSString *copyStringverse = yourSelectedOrClickedTableViewCell.textLabel.text;
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:copyStringverse];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • haii thanks for your reply,what s yourSelectedOrClickedTableViewCell ?table name? – stackiphone Jan 15 '12 at 12:31
  • @Micheal Dautermann i need to put it inside the butytonclick,so the text label shows eror – stackiphone Jan 15 '12 at 12:32
  • `yourSelectedOrClickedTableViewCell` is a `UITableViewCell`. You have to figure out which cell is being clicked. I don't know what the rest of your code looks like, so I don't know how you would invoke the copying and I couldn't come up with a better code sample. – Michael Dautermann Jan 15 '12 at 12:33
27
[UIPasteboard generalPasteboard].string = @"Copy me!";
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
Aqib Mumtaz
  • 4,936
  • 1
  • 36
  • 33
15

For Swift 3.x

UIPasteboard.general.string = "String to copy"
chrisamanse
  • 4,289
  • 1
  • 25
  • 32
4

For Swift 2.1+:

let cell = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell // change this to your custom cell if you use one
UIPasteboard.generalPasteboard().string = cell.textLabel.text
jaytrixz
  • 4,059
  • 7
  • 38
  • 57
1

For Swift2.2

UIPasteboard.generalPasteboard().string = tableViewCell.textLabel.text

By using this you can directly set the value to UIPasteboard.

Ramakrishna
  • 712
  • 8
  • 26