简体   繁体   English

无法更改 NSTableColumn 的列宽

[英]Can't change column width of NSTableColumn

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.如本示例所示,我已将NSTableView嵌入到NSScrollView中,但出于某种原因,设置列宽仅在初始设置时才能正常工作。 Changing it later, ie 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.在这些调用col.width正确返回 1000 之后,但NSTableView不显示更改。 It still looks as before, ie longer entries are still cut off using ... even though the column width is 1000 points now.它看起来仍然和以前一样,即较长的条目仍然被使用...切断,即使列宽现在是 1000 点。

I have tried to call [tableView setNeedsDisplay:YES] after changing the column width but it doesn't help.我试图在更改列宽后调用[tableView setNeedsDisplay:YES]但它没有帮助。 Calling setNeedsDisplay on the NSScrollView doesn't help either.NSScrollView上调用setNeedsDisplay也无济于事。 I've also tried playing with NSTableColumn 's resizingMask and NSTableView 's columnAutoresizingStyle but all to no avail.我也试过玩NSTableColumnresizingMaskNSTableViewcolumnAutoresizingStyle但都无济于事。 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];

Here's one technique to change the column width of a table view.下面是一种更改表视图列宽的技术。

#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;
}

It looks like a bug to me.对我来说它看起来像一个错误。 [col setMinWidth:1000] also sets the width to 1000 but doesn't update the table view. [col setMinWidth:1000]还将宽度设置为1000但不更新表格视图。 [col setWidth:1000] doesn't do anything because the width is 1000 . [col setWidth:1000]不做任何事情,因为宽度是1000 Fix: set the width first:修复:先设置宽度:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM