Corbin Dunn and Louise Lovelle
Treehouse
Woodworking
Pictures/Photography
Videos
Machining
About
Search

Archive for July, 2005

Drag and Drop in an NSTableView

Friday, July 29th, 2005

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 )info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op {

    // 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 )info row:(int)row dropOperation:(NSTableViewDropOperation)op {

    NSLog(@“acceptDrop”);

    // Add code here to accept the drop

    return YES;    

}

And that is it! It is pretty easy…

Technorati Tags:

Dynamically populating an NSPopUpButtonCell in an NSTableView

Wednesday, July 27th, 2005

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:

Popupcellpicture1

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:

Menusdelegatepopupbutton

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:

Picture 1-3

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:

Popupcellhookup

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

Wednesday, July 20th, 2005

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

Wednesday, July 20th, 2005

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”.


(c) 2008-2009 Corbin Dunn

Corbin’s Treehouse is powered by WordPress. Made on a Mac.

Entries (RSS) and Comments (RSS).

18 queries. 0.706 seconds.