NSTableView selected row foreground color

danipendanipen USMember ✭✭

Hi,

I'm following an example described in the documentation: https://developer.xamarin.com/guides/mac/user-interface/working-with-tableviews/ to build a simple application with a NSTableView inside.

When I select a row in Mountain Lion (10.8), as you can see in the attached image (mountain_lion.png), the foreground color of the selected row is black.
However, when I execute the same example in El Capitan (10.11), the foreground color of the hightlighted row is white, as you can see in the other attached image (elcapitan.png).

Do you know why I am experiencing this behavior? Is there anything I can do to fix this issue?

Thanks in advance.

Tagged:

Best Answer

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    That sounds like Cocoa behavior they changed between the two. I do not believe Xamarin.Mac has anything to do with the drawing changes.

    You could look into changing this draw behavior if that is undesirable. http://www.corbinstreehouse.com/blog/2008/01/cocoa-willdisplaycell-delegate-method-of-nstableview-nscell-settextcolor-and-source-lists/ suggests possible routes forward.

    Since it is written in objective-c, you may need to review this documentation to translate:

    https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/
    https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Do you know why I am experiencing this behavior?

    I believe it's for the same reason the window buttons on Mountain Lion are shiny, 3D buttons, but on El Capitan they're non-shiny, non-3D buttons - namely Apple decided to change it.

    The value comes from the system color settings. They're meant to be set by the user and apps should use them rather than defining their own. I'm sure there's a way to override it, but I don't have any answer off the top of my head.

  • danipendanipen USMember ✭✭
    edited August 2016

    Hi @ChrisHamons . Thanks for your answer.

    I'm playing with the sample you did provided, but unfortunately I can't change the drawing behavior.

    I'm trying to override the WillDisplayCell and WillDisplayCell methods in my NSTableViewDelegate implementation, but those methods are never called in my Xamarin application. I attach my source code example, so you maybe can understand what is happening. I'm lost right now.

    Is there any flag or something that should I set on my application to get this working?

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    @danipen - Congrats, you fell into the same exact pit of pain that I ran into when I first messed with NSTableView.

    While it exists in the Apple documentation, the difference between NSCell and NSView based table's is not super called out. Let me give you the tl;dr:

    • A NSTableView can be either in "cell based" or "view based", depending on what methods you implement.
    • A "cell based" table will call methods based on returning NSCell derived instances while a "view based" one will return NSView.
    • Since your delegate has NSView GetViewForItem () you are a "view based" one.
    • You really want to be view based if you can get away with it. In multiple WWDC sessions, the feeling that has come out is that view based is the future, it will get improvements and optimizations, and NSCell APIs are the old and busted way of doing things.
    • Because you are "view based", the Cell APIs WillDisplayCell and GetDataCell do not get called.
    • And yes, it is crazy that Apple didn't create a new protocol to the cell and view based methods into two nice groups. And confusing. :(

    You likely to make a NSTableCellView derived view, return that in GetViewForItem, and override it's WillDisplay method.

  • danipendanipen USMember ✭✭

    Wow. Thank you very much for the information. Now things start to make sense.

    Assuming that I want to implement a NSView-based table, what would be the steps to highlight in white the selected rows? AFAIK, this is something that cocoa should done automatically (in fact it is done in El Capitan, but it does not work for Mountain Lion). Is there any workaround to make it work in Mountain Lion?

Sign In or Register to comment.