I have embedded an NSTableView
inside an NSScrollView
as shown in this example but for some reason, setting the column width only works correctly when initially setting it. Changing it later, i.e. in response to a button click, doesn't do anything at all. I do it like this:
[col setMinWidth:1000];
[col setMaxWidth:1000];
[col setWidth:1000];
After those calls col.width
correctly returns 1000 but the NSTableView
doesn't show the change. It still looks as before, i.e. longer entries are still cut off using ...
even though the column width is 1000 points now.
I have tried to call [tableView setNeedsDisplay:YES]
after changing the column width but it doesn't help. Calling setNeedsDisplay
on the NSScrollView
doesn't help either. I've also tried playing with NSTableColumn
's resizingMask
and NSTableView
's columnAutoresizingStyle
but all to no avail. Trying to change the column width just doesn't work here. Any ideas?
EDIT
Here's the code for reference:
listDelegate = [[MyListDelegate alloc] initWithChoices:array];
scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(20, 52, rect.size.width - 2 * 20, 200)];
tableview = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width - 2 * 20 - 16, 200)];
column = [[NSTableColumn alloc] initWithIdentifier:@"Column"];
[column setWidth:400];
[tableview addTableColumn:column];
[tableview setHeaderView:nil];
[tableview setDelegate:listDelegate];
[tableview setDataSource:listDelegate];
[tableview reloadData];
[scrollview setDocumentView:tableview];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:YES];
[[win contentView] addSubview:scrollview];
[scrollview release];
[column release];
The list delegate looks like this:
@interface MyListDelegate : NSObject
{
NSArray *choices;
}
- (id)initWithChoices:(NSArray *)c;
@end
@implementation MyListDelegate
- (id)initWithChoices:(NSArray *)c
{
if(!(self = [super init])) return nil;
choices = c;
return self;
}
- (int)numberOfRowsInTableView:(NSTableView *)_tableView
{
return (int) [choices count];
}
- (id)tableView:(NSTableView *)_tableView objectValueForTableColumn:(NSTableColumn *) tableColumn row:(int)row
{
return [choices objectAtIndex:row];
}
- (BOOL)tableView:(NSTableView *)_tableView shouldEditTableColumn:(NSTableColumn *) tableColumn row:(int)row
{
return NO;
}
@end
And when the button is pressed, the following code is executed:
NSTableColumn *col = [[tableview tableColumns] objectAtIndex:0];
[col setMinWidth:1000];
[col setMaxWidth:1000];
[col setWidth:1000];