没有使用NIB文件的NSOutlineView源列表

3
我正在尝试完全使用代码来创建源列表。我一直在研究SideBar演示和各种SO问题/答案,并且已经接近成功,但还没有完全解决。我复制了下面的代码,它似乎可以构建一个视图,其中有3个空单元格可以选择。我无法弄清楚的是如何在这些单元格中显示我的文本。最终我将用自己的视图替换它,但现在我只想让股票渲染工作。
在我的控制器中:
@implementation SourceListController {
    NSScrollView *_scrollView;
    NSOutlineView *_sourceList;
    SourceListDataSource *_sourceListDataSource;
}

- (void)loadView {
    _scrollView = [[NSScrollView alloc] init];

    _sourceList = [[NSOutlineView alloc] init];
    _sourceListDataSource = [[SourceListDataSource alloc] init];

    [_sourceList setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
    NSTableColumn *c = [[NSTableColumn alloc] initWithIdentifier: @"Column"];
    [c setEditable: NO];
    [c setMinWidth: 150.0];
    [_sourceList addTableColumn: c];
    [_sourceList setOutlineTableColumn:c];
    [_sourceList setDelegate:_sourceListDataSource];
    [_sourceList setDataSource:_sourceListDataSource];

    [_scrollView setDocumentView:_sourceList];
    [_scrollView setHasVerticalScroller:YES];

    [_sourceList reloadData];

    NSLog(_sourceList.dataSource == _sourceListDataSource ? @"YES" : @"NO");

    self.view = _scrollView;

}

在我的数据源/代理中:

@implementation SourceListDataSource {

}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    return item == nil ? 3 : 0;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    return [NSObject new];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return item == nil;
}

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    NSLog(@"Here %f", tableColumn.width);
    NSTableCellView *result = [[NSTableCellView alloc] init];
    NSTextField *textField = [[NSTextField alloc] init];
    [textField setStringValue:@"Test"];
    [result addSubview:textField];
    return result;
}

更新:进一步调查告诉我viewForTableColumn甚至没有被调用。我对此感到困惑。

更新2:我发现问题出在缺少列和outlineTableColumn导致未调用委托方法。我修复了这个问题并更新了代码,但仍然没有文本显示。


NSTableView和NSOutlineView默认使用“基于单元格”的内容类型。将最后一个数据源方法替换为-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item是否会返回任何内容? - Cory
不,那个方法也没有被调用。 - XeroxDucati
从您的编辑中我了解到委托方法被调用,但是没有单元格视图显示? - Volker
正确。我没有任何视图。 - XeroxDucati
一个标准的NSTableCellView已经包含一个名为textField的属性。也许这样就可以了。你添加的字段没有框架,因此无法显示。 - Volker
@XeroxDucati,我刚发布完这个问题就看到了这个链接:http://stackoverflow.com/questions/24774492/viewfortablecolumn-not-being-called-for-child-items-in-view-based-nsoutlineview。我认为我们的问题是一样的。你有没有找到解决方案?谢谢。 - jkcl
1个回答

0

你的委托方法需要进行更改,否则它将无法正常工作,并且也没有使用新的视图缓存方法。大多数情况下,应该使用NSTableCellView.textField属性,而不是添加自己的没有框架和框架约束的NSTextField。因此,应该像这样编写代码:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {

    NSTableCellView *result = [outline makeViewWithIdentifier:@"MyView" owner:self];
    if (result == nil) {

         // Create the new NSTextField with a frame of the {0,0} with the width of the table.
         // Note that the height of the frame is not really relevant, because the row height will modify the height.
         CGRect myRect = CGRectMake(0,0,outlineView.framesize.width,0);
         result = [[NSTableCellView alloc] initWithFrame:myRect];

         // The identifier of the view instance is set to MyView.
         // This allows the cell to be reused.
         result.identifier = @"MyView";
    }

    result.textField.stringValue = @"Test";
    return result;
}

以上所有内容在SO中输入。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接