【问题标题】:NSBrowser image is not displaying in cellNSBrowser 图像未显示在单元格中
【发布时间】:2020-03-12 13:53:17
【问题描述】:

我正在使用 NSBrowser 视图来显示查找器类型应用程序中的文件和文件夹列表。我正在为 NSBrowser 使用新的 Item Base 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;

}

Apple SimpleCocoaBrowser Example

【问题讨论】:

    标签: cocoa osx-yosemite nsbrowser


    【解决方案1】:

    虽然 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

    【讨论】:

      【解决方案2】:

      使用 catlan 的答案中描述的 NSBrowserCell 有效,但是 NSTextField 在绘制选定单元格的背景时的行为与 NSBrowserCell 不同。当鼠标在另一列中单击/拖动时,NSBrowserCell 将绘制所选单元格的背景,该背景为蓝色,而不是灰色(Finder 也这样做)。但是 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];
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多