简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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