首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元格中未显示NSBrowser图像

单元格中未显示NSBrowser图像
EN

Stack Overflow用户
提问于 2015-08-03 16:45:25
回答 2查看 271关注 0票数 3

我正在使用NSBrowser视图来显示查找器类型Api中的文件和文件夹列表。我正在为NSBrowser使用新的项库Api。

问题是当我尝试在willDisplayCell方法中设置图像时。视图中未显示任何内容。

代码:

代码语言:javascript
复制
// 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;

}

Apple SimpleCocoaBrowser Example

EN

回答 2

Stack Overflow用户

发布于 2019-01-10 21:16:13

虽然cellPrototype的默认值是NSBrowserCell,但它似乎使用了NSTextFieldCell。(macOS 10.14)

要解决这个问题,您需要子类NSBrowserCell,并将子类设置为cellClass:[_browser setCellClass:[BrowserCell class]];

代码语言:javascript
复制
@interface BrowserCell : NSBrowserCell
@end

@implementation BrowserCell
@end

另一个问题是叶指示器。它将显示两次,一次来自单元格,一次来自浏览器。

代码语言:javascript
复制
- (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

票数 2
EN

Stack Overflow用户

发布于 2020-03-12 13:53:17

使用catlan的答案中描述的NSBrowserCell是可行的,但在绘制选定单元格的背景时,NSTextField的行为与NSBrowserCell不同。当鼠标在另一列中单击/拖动时,NSBrowserCell将绘制选定单元格的背景,而不是蓝色作为灰色(查找器也会这样做)。但是,单击鼠标时NSTextFieldCell将保持蓝色,松开鼠标后将变为灰色。因为叶指示器不是由NSBrowserCell绘制的,所以单元格的该区域仍将具有蓝色选择高亮,因此单元格同时具有蓝色和灰色作为背景颜色。这是简短的,因为它只是在点击的时候,但它看起来确实是错误的。

要让NSBrowserCell的行为像NSTextFieldCell一样,需要一些逆向工程和私有API,所以我决定做正确的事情就是将NSTextFieldCell子类,并在其中绘制一个图标。代码是这样的,

代码语言:javascript
复制
@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];
}
@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31783007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档