根据以下示例,如果最大的 cellSize.width 向上取整,则 [cell cellSize].width 似乎可用于设置基于单元格的表格视图的列宽。通过将'main.m'替换为以下内容并删除预先提供的AppDelegate,可以在Xcode中运行源代码。
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource> {
NSWindow *window;
}
@property(strong, nonatomic) NSArray *teams;
- (void) createMenu;
- (void) createWindow;
@end
@implementation AppDelegate
- (id)init{
if(self = [super init])
_teams = [NSArray arrayWithObjects:@"Ravens", @"Chiefs", @"Chargers", @"Bengals", nil];
NSLog(@"teams = %@",_teams);
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
NSLog(@"count = %lu", [_teams count]);
return [_teams count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSString *team = [_teams objectAtIndex:row];
NSLog(@"team = %@",team);
return [_teams objectAtIndex:row];
}
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)col row:(NSInteger)row {
NSTextFieldCell *cell = [col dataCell];
[cell setBackgroundColor: [NSColor redColor]];
[cell setDrawsBackground:YES];
NSSize size = [cell cellSize];
NSLog(@"cell width = %0.02f",size.width);
return cell;
}
- (void) createMenu {
NSMenu *menubar = [NSMenu new];
NSMenuItem *appMenuItem = [NSMenuItem new];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [NSMenu new];
NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
}
- (void) createWindow {
#define _wndW 300
#define _wndH 300
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle:@"Test window"];
[window makeKeyAndOrderFront: nil];
// **** Table View with Scroll **** //
NSScrollView * scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect( 20, _wndH - 230, _wndW - 40, 200)];
[scrollView setBorderType:NSBezelBorder];
[scrollView setHasVerticalScroller:YES];
[scrollView setAutohidesScrollers:YES];
NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 180, 200)];
NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:@"Col1"];
[[column1 headerCell] setStringValue:@"Teams"];
//Change this to 59 to see ellipsis : longest [cell cellSize].width = 59.33
[column1 setWidth: 60];
[tableView addTableColumn:column1];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView reloadData];
// **** Embed table view into scroll view, and add scroll view to window
[scrollView setDocumentView:tableView];
[[window contentView] addSubview:scrollView];
// ***** Quit btn ***** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self createMenu];
[self createWindow];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification {
}
@end
int main() {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *myDelegate = [[AppDelegate alloc] init];
[application setDelegate:myDelegate];
[application run];
return 0;
}