Search code examples
objective-ccocoanstableviewnstokenfield

NSTokenFieldCell in NSTableView crashes when tabbing, is it a bug?


I was having trouble with NSTokenFieldCell, so I proceeded to create a new project in Xcode to isolate the problem. Here is what I did:

  • Dropped a NSTableView into the main window;
  • selected the second column's text cell, and changed it's Class (via Identity Inspector) to NSTokenFieldCell;
  • implemented a minimum possible data source object, with the following code:

    - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
        return 1;
    }
    
    - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
        return @"aa, bb";
    }
    

At first it seems to work fine, but if you double-click a cell to edit, then tab and shift+tab to switch cells back and forth, eventually the application crashes with a BAD ACCESS when the token field cell receive focus.

I'm using Xcode 4.2 in Lion 10.7.2, with all the default settings that come with a Mac OS X Cocoa Application template.


Solution

  • Looks like a bug in Cocoa. If you turn on zombies you'll see this:

    2011-10-31 00:02:43.802 tokenfieldtest[35622:307] *** -[NSTokenFieldCell respondsToSelector:]: message sent to deallocated instance 0x1da761f10
    

    I tried setting a delegate for the table and implementing - (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row, returning a new NSTokenFieldCell every time (for just the token column), but I got the same error.