Drag and Drop in an NSTableView
Drag and Drop in an NSTableView is easy to do. However, I think the documentation (Table Views: Using Drag and Drop in Tables) for it isn’t particularly great. It misses a few points, so I’m going to go over the basic steps on how to add drag and drop to your TableView. Here, I’ll assume you have a TableView with your source code controller class set as the delegate.
Declare your custom pasteboard format:
#define BasicTableViewDragAndDropDataType @“BasicTableViewDragAndDropDataTypeâ€
In awakeFromNib you must register for the drag types you want to receive (you could have others here):
- (void)awakeFromNib {
  [myTableView registerForDraggedTypes:[NSArray arrayWithObjects:BasicTableViewDragAndDropDataType, nil]];
}
Then, you must implement writeRowsWithIndexes to add your data to the pasteboard:
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
  // Drag and drop support
  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  [pboard declareTypes:[NSArray arrayWithObject:BasicTableViewDragAndDropDataType] owner:self];
  [pboard setData:data forType:BasicTableViewDragAndDropDataType];
  return YES;
}
Next you will validate the drop:
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id
  // Add code here to validate the drop
  NSLog(@“validate Dropâ€);
  return NSDragOperationEvery;  Â
}
Finally, you must have a method that accepts the drop. Here you could access the BasicTableViewDragAndDropDataType and look at the rows that were dragged.
- (BOOL)tableView:(NSTableView*)tv acceptDrop:(id
  NSLog(@“acceptDropâ€);
  // Add code here to accept the drop
  return YES;  Â
}
And that is it! It is pretty easy…
Technorati Tags: Cocoa
Dynamically populating an NSPopUpButtonCell in an NSTableView
It is quite common. You have a PopUpButton (NSPopUpButtonCell) in an NSTableView and you want to dynamically change the contents based on the selected row:

How do you do this? There are a few tricky steps. First, add a menu to the nib and set the delegate for the menu to be your Controller:

Okay. Next is the tricky part. In IB, when you have a column in a tableview selected, it has a little white triangle in the corner:
![]()
Clicking on that will allow you to modify properties of the cell (in this case, the NSPopUpButtonCell). However, we want to set the menu for the NSPopUpButtonCell, so drag from that little triangle to your menu and set the menu outlet:

Okay, good! Now, all your controller needs is a little bit of code to dynamically populate the menu:
- (void)menuNeedsUpdate:(NSMenu *)menu {
  // remove all previous items
  while ([menu numberOfItems] > 0) {
    [menu removeItemAtIndex:0];
  }
  // dynamically build the menu
  int i;
  int selectedRow = [tableViewStuff selectedRow];
  for (i = 0; i <= selectedRow; i++) {
    NSMenuItem *item = [[NSMenuItem alloc]Â
      initWithTitle:[NSString stringWithFormat:@“Menu %dâ€, i]Â
          action:@selector(menuClicked:) keyEquivalent:@“â€];
    [item setTarget:self];
    [menu addItem:item];
  }
}
Okay, that should work, right? Normally, yes. But in a tableview when a cell is tracked, it is first copied. Unfortunately, a small bug in the menu code doesn’t copy the delegate. Therefore, we must fix it up in the nstableview’s delegate method:
- (void)tableView:(NSTableView *)tableViewÂ
 willDisplayCell:(id)cellÂ
  forTableColumn:(NSTableColumn *)tableColumnÂ
       row:(int)rowÂ
{
  if ([cell isKindOfClass:[NSPopUpButtonCell class]]) {
    [[cell menu] setDelegate:self];
  }
}
That’s it! Have fun…happy coding.
Delphi easter eggs
I can’t believe people found out about the Kitchen sink!
http://www.blong.com/Undocumented/Delphi2005.htm
I did write that silly easter egg.
Discovery HD — Weird Homes
Hi All,
Someone just emailed me saying they saw the episode of “Weird Homes†with me in it. It appears to be airing right now! Discovery HD Theater channel. here’s info:
http://dhd.discovery.com/schedule/episode.jsp?episode=26&cpi=82216&gid=0
Cool, huh! I’m the “busy builder who swings through the treesâ€.