我试图找出如何在代码中100%执行源列表。我一直在攻击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导致不调用委托方法。我修复了这个并更新了代码,但仍然没有显示任何文字。
答案 0 :(得分: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中输入。