Simple(?) NSTableView setBackgroundColor issue

Discussion in 'Mac Programming' started by beg_ne, Dec 15, 2007.

  1. beg_ne macrumors 6502

    Jul 3, 2003
    I am currently working through Cocoa Programming for OS X and have reached to the end of Chapter 10 where you work with the User Defaults and set a custom background color for a table view with a pref panel.

    So far I have been unable to get that to work! I always get the default background colors. I've checked the API reference and everything is still intact etc.

    I have used NSLog to output the values in the MyDocument.m file and it looks like its being set, but I'm not seeing the results in the window.

    Here is the code from the -windowControllerDidLoadNib method:
    - (void)windowControllerDidLoadNib:(NSWindowController *) aController
    NSData *colorAsData;
    [super windowControllerDidLoadNib:aController];

    colorAsData = [[NSUserDefaults standardUserDefaults]

    NSLog(@"Current Table Color:%@ : New Table Color Will Be: %@",[tableView backgroundColor],[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]);

    [tableView setBackgroundColor:[NSKeyedUnarchiver
    NSLog(@"New Table Color:%@",[tableView backgroundColor]);

    And here is the log output:
    2007-12-16 00:28:45.248 RaiseMan[7922:10b] Current Table Color:NSNamedColorSpace System controlBackgroundColor : New Table Color Will Be: NSCalibratedRGBColorSpace 1 0.765541 0.607648 1

    2007-12-16 00:28:45.249 RaiseMan[7922:10b] New Table Color:NSCalibratedRGBColorSpace 1 0.765541 0.607648 1

    What am I doing wrong???
  2. titaniumdecoy macrumors member

    Oct 13, 2005
    Perhaps you need to call [tableView setNeedsDisplay:YES]
  3. beg_ne thread starter macrumors 6502

    Jul 3, 2003
    Thanks for the suggest, I've added that but still no luck. Does this work in Leopard at all? I've ever tried messing with the parent NSScrollView and Table view options for the background color in IB3 and I don't get anything other than the default white background.
  4. bheebn01 macrumors newbie

    Jun 10, 2008
    ah ha!

    I had the same problem. Turn off "Alternating Rows" on the NSTableView. I know it's cool, but it'll allow the example to work as intended.
  5. antibact1 macrumors 6502

    Jun 1, 2006
  6. bobrik macrumors member

    Apr 13, 2007
    Prague, Czech Republic

    Honestly, I don't understand Cocoa yet (want to learn in the summer), but I can see that it's not a good thing to call the same function twice as you do (ever heard of DRY principle?), and not for performance reason, but for the ability to change the function call in one place only (also makes your code more readable). I would write like this:

    userColor = [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
    NSLog(@"Current Table Color:%@ : New Table Color Will Be: %@",[tableView backgroundColor],userColor);
    [tableView setBackgroundColor:userColor];

    I remember that rewriting a code to a seemingly equivalent version (like this) once fixed a bug in code I did not understand. But sorry I probably did not solve your problem.
  7. Eraserhead macrumors G4


    Nov 3, 2005
    One of them is a log (the NSLog one) so will be removed from the final code. So you don't actually want to create a variable for it.
  8. omaha macrumors newbie

    Dec 29, 2009
    I had the same problem. The way I fixed it is go back to Chapter 9 "NSUndoManager" page 150, set File's Owner "window" outlet to "tableView"

    Previously I had some difficulty to select "tableView" in Interface Builder (due to my own ignorance).

    After that change the color does work.


Share This Page