Search code examples
nstableviewnstableviewcell

NSComboBoxCell won't display values


So I have a perfect working NSTableView for a Mac App. I added a new column on it, and I am trying to set it as Combo, but I am facing a lot of issues. First is, if I drag and drop the NSComboBoxCell on the Storyboard, it will lose all my other columns (weird thing #1). So I decided to overwrite my NSTextFieldCell, which looks like is the way to go. But now the items on my combobox are not showing up.

- (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
 NSTableCellView *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];    

if ([tableColumn.identifier isEqualToString:@"typeCell"]) {
    NSComboBoxCell *combo =(NSComboBoxCell*) [cell.textField viewWithTag:10];

    [combo addItemsWithObjectValues:@[@"Test"]];

}

Any idea on what I am doing wrong? The column is also already editable.


Solution

  • You're confusing NSTableCellView (a subclass of NSView, used for cells in a view based table view) and NSTextFieldCell (a subclass of NSCell, used for cells in a cell based table view).

    Solution to weird thing 1: drag a NSComboBox (without Cell) to the table view.

    viewForTableColumn should return a (subclass of) NSView, not a (subclass of) NSCell.

    [cell.textField viewWithTag:10] returns a subview of a NSTextField. You shouldn't put subviews in NSTextFields and I don't think you did.

    the items on my combobox are not showing up

    combo is nil.