我正在使用NSBrowser视图来显示查找器类型Api中的文件和文件夹列表。我正在为NSBrowser使用新的项库Api。
问题是当我尝试在willDisplayCell方法中设置图像时。视图中未显示任何内容。
代码:
// This is a utility method to find the parent item for a given column. The item based API eliminates the need for this method.
- (FileSystemNode *)parentNodeForColumn:(NSInteger)column {
if (_rootNode == nil) {
_rootNode = [[FileSystemNode alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/kiritvaghela"]];
}
FileSystemNode *result = _rootNode;
// Walk up to this column, finding the selected row in the column before it and using that in the children array
for (NSInteger i = 0; i < column; i++) {
NSInteger selectedRowInColumn = [_browser selectedRowInColumn:i];
FileSystemNode *selectedChildNode = [result.children objectAtIndex:selectedRowInColumn];
result = selectedChildNode;
}
return result;
}
- (void)browser:(NSBrowser *)browser willDisplayCell:(NSBrowserCell *)cell atRow:(NSInteger)row column:(NSInteger)column {
FileSystemNode *parentNode = [self parentNodeForColumn:column];
FileSystemNode *childNode = [parentNode.children objectAtIndex:row];
[cell setTitle:childNode.displayName];
cell.image = node.icon;
}发布于 2019-01-10 21:16:13
虽然cellPrototype的默认值是NSBrowserCell,但它似乎使用了NSTextFieldCell。(macOS 10.14)
要解决这个问题,您需要子类NSBrowserCell,并将子类设置为cellClass:[_browser setCellClass:[BrowserCell class]];
@interface BrowserCell : NSBrowserCell
@end
@implementation BrowserCell
@end另一个问题是叶指示器。它将显示两次,一次来自单元格,一次来自浏览器。
- (void)browser:(NSBrowser *)browser willDisplayCell:(NSBrowserCell *)cell atRow:(NSInteger)row column:(NSInteger)column {
FileSystemNode *parentNode = [self parentNodeForColumn:column];
FileSystemNode *childNode = [parentNode.children objectAtIndex:row];
NSImage *image = node.icon;
[image setSize:NSMakeSize(16, 16)];
cell.image = cell.image;
cell.leaf = YES;
}雷达://47175910
发布于 2020-03-12 13:53:17
使用catlan的答案中描述的NSBrowserCell是可行的,但在绘制选定单元格的背景时,NSTextField的行为与NSBrowserCell不同。当鼠标在另一列中单击/拖动时,NSBrowserCell将绘制选定单元格的背景,而不是蓝色作为灰色(查找器也会这样做)。但是,单击鼠标时NSTextFieldCell将保持蓝色,松开鼠标后将变为灰色。因为叶指示器不是由NSBrowserCell绘制的,所以单元格的该区域仍将具有蓝色选择高亮,因此单元格同时具有蓝色和灰色作为背景颜色。这是简短的,因为它只是在点击的时候,但它看起来确实是错误的。
要让NSBrowserCell的行为像NSTextFieldCell一样,需要一些逆向工程和私有API,所以我决定做正确的事情就是将NSTextFieldCell子类,并在其中绘制一个图标。代码是这样的,
@implementation BrowserTextFieldCell
- (void)drawInteriorWithFrame:(NSRect)cellFrame controlView:(NSView *)controlView
{
__auto_type textFrame = cellFrame;
__auto_type inset = kIconHorizontalPadding * 2 + kIconSize;
textFrame.origin.x += inset;
textFrame.size.width -= inset;
[super drawInteriorWithFrame:textFrame inView:controlView];
[self drawIconWithFrame:cellFrame];
}
- (void)drawIconWithWithFrame:(NSRect)cellFrame
{
NSRect iconFrame = cellFrame;
iconFrame.origin.x += kIconPadding;
iconFrame.size = NSMakeSize(kIconSize, kIconSize);
[self.iconImage drawInRect:iconFrame];
}
@endhttps://stackoverflow.com/questions/31783007
复制相似问题