user718408
user718408

Reputation: 101

Programmatically-created NSTableView does not display

I want to create a tableview in code, not IB, but it will not display. Here is my code:

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

When I debugged, I see it doesn't run objectValueForTableColumn:. I don't know why.

Upvotes: 1

Views: 1171

Answers (1)

user557219
user557219

Reputation:

You haven’t added any table column to the table view. Use -[NSTableView addTableColumn:] for that.

Also, I’m not sure what you expect by returning a cell as the object of a row & column. That method should return an object that’s suitable to be displayed by the cell in that row & column.

Upvotes: 3

Related Questions