Copy and paste from NSTableView

  • I'm trying to add support for copy and paste from an NSTableView
    and could use a hint on where and how to intercept the Copy command
    from the Edit menu to put the selected data into the NSPasteBoard.

    Thanks,

    - Peter
    --
  • These menus send their actions to the first responder.  You need
    to put a method that handles "copy" and "paste" in your first
    responder chain, such as your window's delegate.  Then just
    implement the action, e.g.:

    - (IBAction) copy:(id)sender
    {
    [oMyTable copySelectedRow];  // see below
    }

    Here's a category I added to NSTableView for handling copy.  It
    puts the columns into two different pasteboard formats; this may
    or may not be what you actually want.

    Paste is left as an exercise for the reader....

    @implementation NSTableView ( clipboard )

    /*"    Copy the single selected row from the table.  The elements
    are separated by newlines, as text (!{NSStringPboardType}), and
    by tabs, as tabular text (!NSTabularTextPboardType).
    "*/

    - (void) copySelectedRow
    {
    int  selectedRow = [self selectedRow];
    int  numberOfRows = [self numberOfRows];
    if ( selectedRow >= 0 && selectedRow < numberOfRows )
    {
      NSPasteboard *pb = [NSPasteboard generalPasteboard];
      NSMutableString *tabsBuf = [NSMutableString string];
      NSMutableString *textBuf = [NSMutableString string];

      NSArray *tableColumns = [self tableColumns];
      NSEnumerator *enumerator = [tableColumns objectEnumerator];
      NSTableColumn *col;
      id dataSource = [self dataSource];
      while (nil != (col = [enumerator nextObject]) )
      {
      id columnValue
        = [dataSource tableView:self
    objectValueForTableColumn:col row:selectedRow];
      NSString *columnString = @"";
      if (nil != columnValue)
      {
        columnString = [columnValue description];
      }
      [tabsBuf appendFormat:@"%@\t",columnString];
      if (![columnString isEqualToString:@""])
      {
        [textBuf appendFormat:@"%@\n",columnString];
      }
      }
      // delete the last tab.  (But don't delete the last CR)
      if ([tabsBuf length])
      {
      [tabsBuf deleteCharactersInRange:NSMakeRange([tabsBuf length]-1, 1)];
      }
      [pb declareTypes:
      [NSArray arrayWithObjects:NSTabularTextPboardType,
    NSStringPboardType, nil]
      owner:nil];
      [pb setString:[NSString stringWithString:textBuf]
      forType:NSStringPboardType];
      [pb setString:[NSString stringWithString:tabsBuf]
      forType:NSTabularTextPboardType];
    }
    }

    On Thursday, December 6, 2001, at 08:12  PM, Peter Sichel wrote:

    > I'm trying to add support for copy and paste from an NSTableView
    > and could use a hint on where and how to intercept the Copy command
    > from the Edit menu to put the selected data into the NSPasteBoard.
    >
    > Thanks,
    >
    > - Peter
    > -- _______________________________________________
    > cocoa-dev mailing list
    > <cocoa-dev...>
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    >
    >

    --
    Dan Wood
    <dwood...>
    http://www.karelia.com/
    Watson for Mac OS X: http://www.karelia.com/watson/
    Mac OS X Developer: Online Resume: http://www.karelia.com/resume.html
previous month december 2001 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today