NSTableView 中动态创建的列为空

Dynamically created columns in NSTableView are empty

我有一个 NSTableView 连接到我的 CountViewController.h

@property (nonatomic, weak) IBOutlet NSTableView * tableCountData;

-(void)viewDidAppear 下,我有删除所有列的代码,除了第一列,然后用新列重新创建。

-(void)viewDidAppear
{
    NSArray *tableColumns = [self.tableCountData tableColumns];
    BWProject *project = ((Document *)self.view.window.windowController.document).project;

    for (NSTableColumn *col in tableColumns) {
        if ([col.identifier isEqualToString:@"columnGenes"]) continue;
        [self.tableCountData removeTableColumn:col];
    }

    for (NSString *sample in project.samples) {
        NSTableColumn *col = [[NSTableColumn alloc] init];
        col.identifier = sample;
        col.headerCell.stringValue = sample;
        [self.tableCountData addTableColumn:col];
    }

    [self.tableCountData reloadData];
}

但是,当我按如下方式填充 table 时:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    BWProject *project = ((Document *)self.view.window.windowController.document).project;
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

    if (cellView == nil) {
        CGRect myRect = CGRectMake(0,0,tableColumn.width,0);
        cellView = [[NSTableCellView alloc] initWithFrame:myRect];
        [cellView addSubview:[[NSTextField alloc] initWithFrame:cellView.frame]];
    }

    if ([tableColumn.identifier isEqualToString:@"columnGenes"]) {
        cellView.textField.stringValue = [[project.geneset allKeys] objectAtIndex:row];
    } else {
        for (NSString *sample in project.samples) {
            if ([tableColumn.identifier isEqualToString:sample]) {
                BWGene *gene = [project.geneset objectForKey:[[project.geneset allKeys] objectAtIndex:row]];
                BWGeneReads *geneReads = [gene.samples objectForKey:sample];
                cellView.textField.stringValue = [NSString stringWithFormat:@"%f", geneReads.reads];
            }
        }
    }

    return cellView;
}

我得到空数据列。我尝试用 [cellView setTextField:[[NSTextField alloc] initWithFrame:cellView.frame]] 替换 [cellView addSubview:[[NSTextField alloc] initWithFrame:cellView.frame]],但这会导致应用程序崩溃并显示 EXEC_BAD_ACCESS。以下是示例屏幕截图:

我做错了什么?我在文档中找不到任何线索,但我觉得这是一个简单的解决方案,因为许多应用程序都使用动态列...

不要在基于 table 的视图中执行 setDataCell:

如果您在 viewForTableColumn 中创建新的 NSTableCellView,则会添加一个 NSTextField 子视图。您必须将单元格视图的 textField 设置为此文本字段。

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    BWProject *project = ((Document *)self.view.window.windowController.document).project;
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

    if (cellView == nil) {
        CGRect myRect = CGRectMake(0,0,tableColumn.width,0);
        cellView = [[NSTableCellView alloc] initWithFrame:myRect];
        NSTextField *myTextField = [[NSTextField alloc] initWithFrame:cellView.frame];
        cellView.textField = myTextField; // this assignment was missing
        [cellView addSubview:myTextField];
    }

    if ([tableColumn.identifier isEqualToString:@"columnGenes"]) {
        cellView.textField.stringValue = [[project.geneset allKeys] objectAtIndex:row];
    } else {
        for (NSString *sample in project.samples) {
            if ([tableColumn.identifier isEqualToString:sample]) {
                BWGene *gene = [project.geneset objectForKey:[[project.geneset allKeys] objectAtIndex:row]];
                BWGeneReads *geneReads = [gene.samples objectForKey:sample];
                cellView.textField.stringValue = [NSString stringWithFormat:@"%f", geneReads.reads];
            }
        }
    }

    return cellView;
}