OMG, who do I have to beat up to get NSTreeView to work?

Discussion in 'Mac Programming' started by DannySmurf, Jul 7, 2005.

  1. DannySmurf macrumors 6502a

    Jul 7, 2005
    Okay, I'm very close to pitching my Mac out of a Window in frustration here. So I'm kind of hoping that someone knows the answer and can save a perfectly good computer from being smashed on the pavement.

    Here's the situation:

    I want to add a list control to a window. So I figured on using NSTableView. And I've been fighting with it for eight hours trying to get it to do ANYTHING. Every example/tutorial I've been able to find online basically consists of this:

    1. Drop the NSTableView onto the Window in IB
    2. Create a controller class for it derived from NSObject, instantiate and hook up the dataSource outlet for the NSTableView to it
    3. Create the source files and add the numberOfRowsInTable and tableView:eek:bjectValueForTableColumn:row member functions to the controller class
    4. Create an array in the controller class and return appropriate values from both of the above members

    Now, let me be absolutely clear about this: these four steps DO NOT result in a working NSTableView. The controller class is instantiated at run time. Its init function is called, the array is populated. Neither numberOfRowsInTable nor tableView:eek:bjectValueForTableColumn:row are ever called. Calling reloadData on the NSTableView manually does absolutely nothing.

    I don't know if every writer of an example or tutorial is on crack, or if there's something they're keeping to themselves so that they can giggle at my frustration, but something critical IS missing here, because the code simply doesn't work.

    Can someone PLEASE tell me what it is that I need to do (or not do) to make this work? If I need to give Steve Jobs sexual favours, that's fine. If I need to give YOU sexual favours, I'm sure that's doable. But seriously... this is ridiculous. I know this control works, since Apple uses it in its own apps, but I haven't been able to find a single piece of source code that demonstrates how to make it work myself.
  2. HexMonkey Administrator


    Staff Member

    Feb 5, 2004
    New Zealand
    That's strange, as you're not missing any steps. I use NSTableViews extensively and have never had any problems like this.

    Double check everything, ensuring most importantly that the table's dataSource outlet is properly connected. Other than this, the only potential problem I can think of is the source code. How are you checking whether your methods are being called? Also, are there any runtime errors that occur when creating the controller object?

    Try replacing the entire code of the controller's implementation file with the following (replacing "Controller" with the name of your controller class both times it appears). Does anything happen when you run it? Are there any beeps, or is anything printed to the run log?

    #import "Controller.h"
    @implementation Controller
    - (int)numberOfRowsInTableView:(NSTableView *)aTableView
    	return 5;
    - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
    	return @"Test";
    If none of this helps, you could reply with your zipped project files (stripped down if necessary) as an attachment. I (or one of the other members) can then look at the project and hopefully find what the problem is.

    Good luck, and welcome to MacRumors.
  3. DannySmurf thread starter macrumors 6502a

    Jul 7, 2005
    Okay, after getting a night's sleep and then coming back and looking at your example, and then at my own code, I just feel like an idiot. My source included this:

    - (int)numberOfRowsInTableView;

    rather than the obviously necessary:

    - (int)numberOfRowsInTableView:(NSTableView*)aTableView;

    Once I fixed up that mistake, everything works just fine. And I'm sane again. :)

    Thanks for your help, HexMonkey.
  4. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    I hate it when one does something like that. You spend ages looking at your code going 'Why aren't you bloody working?' and it's usually something stupid like a missing connection in IB :(
  5. DannySmurf thread starter macrumors 6502a

    Jul 7, 2005
    Actually, what's even worse is when code that WAS working stops working, and you can't figure out why.

    But the thing that REALLY pisses me off, more than anything in the entire WORLD is inconsistent behaviour in an API. Meaning, you use a function somewhere and it does one thing and you replicate that code line-for-line elsewhere in your app, and it does something different (or worse, crashes).

    Right now, I'm arguing with NSFileHandle over its behaviour. In one part of my app, I use fileHandleForReadingAtPath: to open a file, read part of the file, and close it.

    In another part of my app, I use fileHandleForReadingAtPath: to open a file (the same file, mind you), read part of the file, and close it... and then the app crashes.
  6. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    I once had a problem with a value returned from a function which I used like

    variable = [class method];

    use variable in several lines

    Sadly the value was autoreleased so disappeared up it's own bottom at random locations in the subsequent code. Obviously I should have retained it and it was a 'novice' error but....How long did I look at that code going 'What the hell is going on?'.

    It was a 'learning experience'... :rolleyes:
  7. HiRez macrumors 603


    Jan 6, 2004
    Western US
    You know you can do all this table view data source stuff a lot easier now using Cocoa bindings, right? It may take you some time to learn to use bindings, but you should, as you'll generally write less code and your application will be more flexible. Apple is using bindings more and more in everything they do.
  8. DannySmurf thread starter macrumors 6502a

    Jul 7, 2005
    Thanks for the link. That looks like it'll be a real time saver in the future. It's not much help for me in this particular case unfortunately, since I've already written most of the glue code to do what Cocoa bindings is supposed to do.

Share This Page