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

DannySmurf

macrumors 6502a
Original poster
Jul 7, 2005
628
0
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.
 

HexMonkey

Administrator
Staff member
Feb 5, 2004
2,135
427
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?

Code:
#import "Controller.h"

@implementation Controller

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
	NSBeep();
	NSLog(@"numberOfRowsInTableView:");
	return 5;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	NSBeep();
	NSLog(@"tableView:objectValueForTableColumn:row:");
	return @"Test";
}

@end
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.
 

DannySmurf

macrumors 6502a
Original poster
Jul 7, 2005
628
0
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.
 

caveman_uk

Guest
Feb 17, 2003
2,403
1
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 :(
 

DannySmurf

macrumors 6502a
Original poster
Jul 7, 2005
628
0
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.
 

caveman_uk

Guest
Feb 17, 2003
2,403
1
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:
 

HiRez

macrumors 603
Jan 6, 2004
5,859
1,726
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.
 

DannySmurf

macrumors 6502a
Original poster
Jul 7, 2005
628
0
HiRez said:
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.
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.