开发者

Can I program a table to look like a table

开发者 https://www.devze.com 2022-12-17 09:50 出处:网络
Can I create a table (TableView) and have it display values for three Columns:- Col1,Col2,Col3 with N values under these three columns?

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 Val开发者_开发问答uen 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.


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.


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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号