views:

108

answers:

3

Can I create a table (TableView) and have it display values for three Columns:- Col1,Col2,Col3 with N values under these three columns?

Col1 Col2 Col3
Value1 Value1 Value1
Value2 Value2 value2
Value3 value3 value3
.... ... ...
.... ... ...
.... ... ...
Valuen Valuen Valuen

I cannot now see how I can code a MutableDictionary and/or a MutableArray to do anything like this.

I am new to cocoa programing and objective-c. Any sample to the point code that I can look at?

I would be grateful.

+1  A: 

The keyword you're after here is bindings. Cocoa Bindings can seem a bit like voodoo at first, but it's the de-facto way of exposing the contents of an array to a table view. And it's absolutely marvelous once you get the hang of it.

A google search for NSTableView bindings tutorial brings up a good number of pages. This tutorial from CocoaDevCenter is really quite good (as is most of their stuff - bindings is an advanced topic, some of their earlier guides may be useful, too).

Apple's docs are a bit denser but may be of use, too.

Matt B.
I have read the tutorial once so far. Like most of these tutorials for cocoa thus far, they start and I am with them, then all of a sudden, I get thoroughly lost.I thank you for leading me to it, none the less. I can assure you that I will try my utmost to understand all of it.
earlcenac
I am now STUDYING the tutorial and I am beginning to understand the whole thing a lot better. Bindings, Controllers, Views, etc. I am definitely on the right tract. Thanks Matt.
earlcenac
Great! It definitely took me quite some time to get a handle on bindings. The hardest part about them is that they're really difficult to troubleshoot if you wire things up incorrectly. I had to follow a bunch of different tutorials before I really started being confident in creating own designs. Best of luck!
Matt B.
+3  A: 

Matt B.'s answer is technically correct, but I would say that for someone who is "new to cocoa programming and objective-c" that bindings might be too much magic to take in at first. (Heck, I've been doing Cocoa stuff for 2 years and I'm just starting to get the hang of them)

I'd say the first thing to understand is the delegate pattern. This pattern is very frequently used in the Cocoa frameworks, and to manually populate an NSTableView with objects, you'll need to understand it.

The basic idea is that the NSTableView knows how to draw, but not what to draw. So what we do is we give the NSTableView a "delegate" object, and the tableview asks the delegate how many rows it has, what goes in each row, etc.

The delegate object itself is an object that knows what should go in the tableview, but not how to draw it. It conforms to NSTableViewDataSource protocol (the equivalent of a Java "interface"). That way the NSTableView has a standard set of methods it can use to query the delegate and ask for the information it needs.

Dave DeLong
Thanks. I am reading about all these topics but I get lost very quickly unless I have simple code and lots of explanation. I can now program in objective-c reasonably ok now. I get throughly lost with topics like views, controllers, delegates, protocols and the like.My problem is understanding the reasons for them. Why they exist in the framework and how exactly do they do what they do. I come from a Delphi background and that kind of programming (tables, StringGrids,DataGrids, etc) is a no brainer in Delphi.
earlcenac
+2  A: 

I cannot now see how I can code a MutableDictionary and/or a MutableArray to do anything like this.

A dictionary is completely useless to you here.

You need to make an object that models what you want to list in the table. Each row corresponds to one of these objects, which you'll most probably keep in an array. The columns correspond to properties of the model objects.

The most common illustration is a list of people (e.g., employees). Each row in the table view displays one Person object. Each column displays one property of that object: First name, last name, perhaps a company title, phone number, etc. Both Bindings and the table view data source protocol are designed to work best (that is, most easily) this way.

You may be tempted to pass on implementing model objects and just write a parallel array or something. As long as you're using a data source, you can do this, but don't fall into this trap—it prevents you from switching to Bindings later, it makes exposing the property to AppleScript (you can't make three arrays look like one property) impossible, and it makes developing the UI beyond a single table view much harder.

Cocoa is designed around Model-View-Controller; work with it, by providing a model, and you'll find everything in Cocoa much easier.

(And no, a dictionary will not suffice as a model object. It won't help you with AppleScript or Bindings, and it'll also fail you any time you want to make the model objects smarter than just a box of simple key-value properties. Derived properties, behavior, and custom initializers are all ugly hacks at best when implemented on dictionaries.)

Peter Hosey
Thanks. I will look up Model-View-Controller implementations in cocoa.By the look of it, I have days of reading and understanding ahead of me. I hope that I can call on you and the other persons that answered my question if I et stuck.
earlcenac
If you get stuck, you should ask another question with your specific problem.
Peter Hosey
I certainly will. So far, since I am studying the thing, I am doing fine.
earlcenac