2012-01-03 9 views
1

私は、ビューベースNSTableViewを使用している、と私は少し問題に出会ってきました。NSTableViewセルの表示の問題

私が強調したときに黒から白に私の2つのラベルのテキストの色を切り替えるようにしようとしています。

そうするために、私は次のコードを書いて、

- (void)tableViewSelectionDidChange:(NSNotification *)notification 
{ 
    NSView * viewInQuestion = [table viewAtColumn:0 row:[table selectedRow] makeIfNecessary:YES]; 

    if ([viewInQuestion isNotEqualTo:lastViewSelected]) 
    { 
     [(NSTextField*)lastViewSelected.subviews.lastObject setTextColor:NSColor.blackColor]; 
     [(NSTextField*)[lastViewSelected.subviews objectAtIndex:1] setTextColor:NSColor.grayColor]; 
    } 

    [(NSTextField*)viewInQuestion.subviews.lastObject setTextColor:NSColor.whiteColor]; 
    [(NSTextField*)[viewInQuestion.subviews objectAtIndex:1] setTextColor:NSColor.whiteColor]; 

    lastViewSelected = viewInQuestion; 
} 

素晴らしい作品。私はこの結果を得る:

問題は時としてテキストがのNSLogがNSTextFieldの色がNSCalibratedWhite(または何でもそれを呼び出して)だったことを私に言っていても白表示されていないことです。

テキストフィールドが表示されていないときの色も黒に切り替わり(バックそれから離れて、次にスクロール)。しかし、これを実行しても、NSTextFieldの色はまだ白として記録されます。

答えて

1

オーバーライドsetBackgroundStyleは、少なくともOS X 10.8上で、私のために完璧に取り組んできました。 (ここで関連する質問の数が前にあったとしたら、これまで何らかの問題があったと推測できます)

バックグラウンドスタイルは、選択イベントとウィンドウのアクティブ化/非アクティブ化時に予想どおりに更新されます。

はここに私のカスタムセルIMPLだ - それが得ることができるよう些細:

@implementation RuntimeInstanceCellView 

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle { 
    [super setBackgroundStyle:backgroundStyle]; 
    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor darkGrayColor] : [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]); 
// self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor blackColor] : [NSColor whiteColor]); 
} 

@end 
+0

を溶液にあなたの答えを変更し、それがバックグラウンドのスタイルに私がやった方法を処理するために、全く最適ではありません。 – evdude100

0

私の方法は、おそらく最適な解決策ではない非常にハックされ、かつ、しかし、それはそれが良いことを解決します。あなたはtableSelectionDidChange私が持っている道実装と仮定

は、あなたがする必要があるすべてはNSNotificationを登録し、より明確でなければなりませんカスタムメソッドを実装しています。 INITで

、目を覚まし、またはアプリケーションの一部didFinishLaunching

...プログラム内の他の

NSView * contentView = table.enclosingScrollView.contentView; 
[contentView setPostsFrameChangedNotifications:YES]; 
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView]; 

どこかで...

(hasUpdatedCellをされたと仮定しBOOLEANプロパティ)

- (void)boundsDidChange:(NSNotification *)notification 
{ 
    /* Bounds can change while nothing is selected--> but we only want to execute the method if a cell is selected. */ 

    if ([table selectedRow] == -1) {return;} 

    NSRect visibleRect = table.enclosingScrollView.visibleRect; 
    NSView * viewInQuestion = [table viewAtColumn:0 row:[table selectedRow] makeIfNecessary:YES]; 
    NSPoint selectedViewOrigin = [viewInQuestion convertPoint:viewInQuestion.frame.origin toView:table.enclosingScrollView]; 

    /* If the selected cell is visible, then we can go ahead and redraw the white text as a part of the workaround. 
     This is because scrolling away from the selected cell and back will make the cell revert back to black. */ 

    BOOL cellVisible = NSPointInRect(selectedViewOrigin, visibleRect); 

    /* We already know we need to update it, and we will so we don't need to evaluate the next step in the program */ 

    if (!cellVisible && !hasUpdatedCell) {return;} 


    if (cellVisible && !hasUpdatedCell) 
    { 
     /* The cell is visible but we haven't updated. Let's do it then. */ 

     [self tableViewSelectionDidChange:nil]; 
     hasUpdatedCell = YES; 
    } 
    else if (!cellVisible) 
    { 
     /* The cell is not visible and we need to update next time. */ 

     hasUpdatedCell = NO; 
    } 
} 

これは正しく表示されるはずです。 NSTableViewCell上