0

NOTE: Andrew's response caused me to take yet another look. This feature is buried deep in a large application which has in internal timer. If that timer is off I get the wrong behavior described here. If the timer is on things work as expected. I don't know why that timer interacts with this table view, but, as I said, it's a large, complex app.

I want the user to be able to select which columns to show. He'll be presented with a set of radio buttons or check boxes, one for each key in the object being displayed. He can check or uncheck a box, and the NSTableView will add or delete a column for that key.

The object being displayed is an NSDictionary. The keys of the dictionary are not known until runtime, so I have to create columns with identifiers at runtime... they are not known at compile time.

Implementing the add functionality went smoothly. I create a column and a header cell, setting the text of the header cell to some value. But the delete is not being so easy. [myTableView removeTableColumn:col] deletes the column, but does not deal with the header so nicely. Sometimes the header text is not removed. Sometimes the header text in two columns is removed. Sometimes I end up with two header text strings printed on top of each other.

Clearly I'm missing something. How does one programmatically remove a column and its header?

garyp
  • 967
  • 7
  • 36
  • Removing the column should also reliably remove the header and its text. Can you post more of your code, or even better, provide a sample project that exposes the problem? – Andrew Madsen May 05 '12 at 13:39
  • I have an app with this functionality. Basically you create the table with all the columns. Then at runtime you can change the visibility of the columns, so you don't actually ever add or delete columns... you just make them visible or not visible. – regulus6633 May 07 '12 at 01:20

2 Answers2

1

It is easily achievable by calling these two methods from check box selector.

To add column dynamically you should call this:

- (void)addColumnWithCheckboxToTable:(NSTableView *)table :(NSString *)header
{
    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:header];
    [[column headerCell] setStringValue:header];

    // Add column to table
    [table addTableColumn:column];
}

And to remove column call this one:

- (void)removeColumnWithCheckboxFromTable:(NSTableView *)table :(NSString *)header
{
    NSTableColumn *column = [table.tableColumns objectAtIndex:[table columnWithIdentifier:header]];
    [table removeTableColumn: column];
}
Gossamer
  • 309
  • 2
  • 16
1

I'm a bit late to this thread but I've recently encounter a very similar situation.

It turned out that in the cases where I was modifying the NSTable structure in the main thread everything worked out fine. At times though the modifications occurred on another thread, which caused the above mentioned problems.

As usual, all the code that modifies the UI should be on the main thread so the fix was relatively simple: have all that code run on the main thread.

aLevelOfIndirection
  • 3,522
  • 14
  • 18