Objective c 以编程方式创建的NSTableView不显示

Objective c 以编程方式创建的NSTableView不显示,objective-c,cocoa,macos,nstableview,Objective C,Cocoa,Macos,Nstableview,我想在代码中创建一个tableview,而不是IB,但它不会显示。这是我的密码: - (void)createTableView{ _tableView = [[NSTableView alloc]initWithFrame:NSMakeRect(0, 0, 700, 300)]; [_tableView setBackgroundColor:[NSColor cyanColor]]; [_tableView setDelegate:self]; [_tableV

我想在代码中创建一个tableview,而不是IB,但它不会显示。这是我的密码:

- (void)createTableView{
    _tableView = [[NSTableView alloc]initWithFrame:NSMakeRect(0, 0, 700, 300)];
    [_tableView setBackgroundColor:[NSColor cyanColor]];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self addSubview:_tableView];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return 10;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSButtonCell *cell = [[[NSButtonCell alloc] init] autorelease];
    [cell setAllowsMixedState:YES];
    [(NSButtonCell *)cell setButtonType:NSSwitchButton];
    [cell setTitle:@"Test"];

    return cell;
}
调试时,我看到它没有运行
objectValueForTableColumn:

我不知道为什么。

您没有在表视图中添加任何表列。使用
-[NSTableView addTableColumn:
进行此操作

另外,我不确定返回一个单元格作为行和列的对象所期望的是什么。该方法应返回适合该行和列中单元格显示的对象