2

NSOutlineviewで、NSTextFieldCellからサブクラス化されたカスタムセルを使用しています。グループ行と通常の行を選択すると、異なる色を描画する必要があります。

そうするために、私は次のことをしました、

-(id)_highlightColorForCell:(NSCell *)cell
{
    return [NSColor colorWithCalibratedWhite:0.5f alpha:0.7f];
}

はい、プライベートAPIは知っていますが、他の方法は見つかりませんでした。これは通常の行では非常にうまく機能していますが、グループ行には影響しません。グループの色を変更する方法はありますか。

よろしくローハン

4

1 に答える 1

4

少なくとも Mac OS X 10.4 以降が必要な場合は、プライベート API に依存せずに実際にこれを行うことができます。

以下を cell サブクラスに入れます。

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
  // Returning nil circumvents the standard row highlighting.
  return nil;
}

次に、NSOutlineView をサブクラス化し、メソッドを再実装します - (void)highlightSelectionInClipRect:(NSRect)clipRect;

非グループ行に 1 つの色を描画し、グループ行に別の色を描画する例を次に示します。

- (void)highlightSelectionInClipRect:(NSRect)clipRect
{
  NSIndexSet *selectedRowIndexes = [self selectedRowIndexes];
  NSRange visibleRows = [self rowsInRect:clipRect];

  NSUInteger selectedRow = [selectedRowIndexes firstIndex];
  while (selectedRow != NSNotFound)
  {
    if (selectedRow == -1 || !NSLocationInRange(selectedRow, visibleRows)) 
    {
      selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow];
      continue;
    }   

    // determine if this is a group row or not
    id delegate = [self delegate];
    BOOL isGroupRow = NO;
    if ([delegate respondsToSelector:@selector(outlineView:isGroupItem:)])
    {
      id item = [self itemAtRow:selectedRow];
      isGroupRow = [delegate outlineView:self isGroupItem:item];
    }

    if (isGroupRow)
    { 
      [[NSColor alternateSelectedControlColor] set];
    } else {
      [[NSColor secondarySelectedControlColor] set];
    }

    NSRectFill([self rectOfRow:selectedRow]);
    selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow];
  }
}
于 2011-02-19T03:36:04.710 に答える