我有一个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];
}
但是,当我按如下方式填充表时:
- (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 addSubview:[[NSTextField alloc] initWithFrame:cellView.frame]]
替换为[cellView setTextField:[[NSTextField alloc] initWithFrame:cellView.frame]]
,但这会导致应用程序因EXEC_BAD_ACCESS而崩溃。以下是一个示例屏幕截图:
我做错了什么?我无法在文档中找到任何线索,但我觉得这是一个简单的解决方案,因为许多应用都使用动态列...
答案 0 :(得分:1)
不要在基于视图的表视图中执行setDataCell:
。
如果您在NSTableCellView
中创建新的viewForTableColumn
,则会添加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;
}