无法更改 NSTableColumn 的列宽

Can't change column width of NSTableColumn

我已将 NSTableView 嵌入 NSScrollView 中,如 example 所示,但出于某种原因,设置列宽仅在初始设置时才能正常工作。稍后更改它,即响应按钮单击,根本不做任何事情。我这样做:

[col setMinWidth:1000];
[col setMaxWidth:1000];
[col setWidth:1000];

在这些调用之后 col.width 正确 returns 1000 但 NSTableView 没有显示更改。它看起来仍然像以前一样,即即使列宽现在是 1000 点,较长的条目仍然使用 ... 截断。

我曾尝试在更改列宽后调用 [tableView setNeedsDisplay:YES],但没有帮助。在 NSScrollView 上调用 setNeedsDisplay 也无济于事。我也试过玩 NSTableColumnresizingMaskNSTableViewcolumnAutoresizingStyle 但都无济于事。尝试更改列宽在这里不起作用。有什么想法吗?

编辑

参考代码如下:

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];

列表委托如下所示:

@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

当按下按钮时,执行以下代码:

NSTableColumn *col = [[tableview tableColumns] objectAtIndex:0];
[col setMinWidth:1000];
[col setMaxWidth:1000];
[col setWidth:1000];

这是一种更改 table 视图的列宽的方法。

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource> {
 NSWindow *window;
 NSTableColumn *column1;
}
- (void) myBtnAction;
- (void) buildMenu;
- (void) buildWindow;
@end

@implementation AppDelegate

- (void) myBtnAction {
 [column1 setWidth:150];
}

- (void) buildMenu {
 NSMenu *menubar = [NSMenu new];
 NSMenuItem *menuBarItem = [NSMenuItem new];
 [menubar addItem:menuBarItem];
 [NSApp setMainMenu:menubar];
 NSMenu *appMenu = [NSMenu new];
 NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
 [appMenu addItem:quitMenuItem];
 [menuBarItem setSubmenu:appMenu];
}

- (void) buildWindow {

#define _wndW  500
#define _wndH  350

window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing: NSBackingStoreBuffered defer: NO];

[window center];
[window setTitle: @"Test window"];
[window makeKeyAndOrderFront: nil];

// **** TableView_SO **** //
 NSScrollView *scrlView = [[NSScrollView alloc] initWithFrame:NSMakeRect(60, 100, 380, 200)];
 NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 364, 200)];
// create columns for our table
 column1 = [[NSTableColumn alloc] initWithIdentifier:@"Col1"];
 NSTableColumn * column2 = [[NSTableColumn alloc] initWithIdentifier:@"Col2"];
 [column1 setWidth:252];
 [column2 setWidth:198];
// generally you want to add at least one column to the table view.
 [tableView addTableColumn:column1];
 [tableView addTableColumn:column2];
 [tableView setDelegate:self];
 [tableView setDataSource:self];
 [tableView reloadData];
// embed table view in the scroll view, and add the scroll view to window.
 [scrlView setDocumentView:tableView];
 [scrlView setHasVerticalScroller:YES];
 [[window contentView] addSubview:scrlView];

// **** Button **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 30, 135, 30 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Change Col1 Width"];
[myBtn setAction: @selector (myBtnAction)];
[[window contentView] addSubview: myBtn];

// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAutoresizingMask: NSViewMinXMargin];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}

- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWindow];
}

- (void) applicationDidFinishLaunching: (NSNotification *)notification {

}
@end

int main (){
 NSApplication *application = [NSApplication sharedApplication];
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 [application setDelegate:appDelegate];
 [application run];
return 0;
}

我觉得这像是一个错误。 [col setMinWidth:1000] 还将宽度设置为 1000 但不会更新 table 视图。 [col setWidth:1000] 不执行任何操作,因为宽度为 1000。修复:先设置宽度:

[col setWidth:1000];
[col setMinWidth:1000];
[col setMaxWidth:1000];