Skip navigation.
 
mlRe: Guidance for Cocoa's steep learning curve
FROM : Jens Alfke
DATE : Fri May 16 04:07:18 2008

On 15 May '08, at 6:33 PM, Joseph Ayers wrote:

> What is absolutely
> baffling is dealing with NSTableView. The documentation absolutely 
> sucks. How does one map table rows and columns
> on NSMutableArrays and NSMutableDictionaries. How does one map the 
> Rows and Columns of a "dataSource"
> on a NSTable view?


I take it you read the table view programming guide and it didn't help?
file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/TableView/Tasks/UsingTableDataSource.html

Basic table display is simple. As that page says:
>> Your data source object can store records in any way you choose, as 
>> long as it is able to identify those records by integer index. It 
>> must also implement methods to provide the following information:
>>
>> * How many records are in the data source (numberOfRowsInTableView 
>> method)
>>
>> * What is the value of a specific record 
>> (tableView:objectValueForTableColumn:row: method)
>>



Lets say you're storing your data as an NSArray of NSDictionaries, 
with each dictionary's keys matching the identifiers of the table 
columns. Then all you have to do is:

- (int) numberOfRowsInTableView: (NSTableView*)table {
   return [_rowArray count];
}

- (id) tableView: (NSTableView*)table objectValueForTableColumn: 
(NSTableColumn*)column row: (int)row {
   NSString *identifier = [tableColumn identifier];
   return [[_rowArray objectAtIndex: row] objectForKey: identifier];
}


> What about records and fields.  Imagine growing up on Excel and then 
> dealing with NSTableView.
> How did this Cocoa NSTableView architecture evolve. Where is the 
> history?


Excel is an application, not an API. All applications present friendly 
interfaces to complex functionality behind the scenes. Welcome to the 
complex functionality :) I'm sure Excel's internal classes or data 
structures are at least as involved as NSTableView.

The key thing is that any generic table view has to support huge 
numbers of rows, and arbitrary ways of storing the data. So the view 
itself can't store the data for you. Instead it has to ask your code 
to fetch each piece of data it needs to display. That's exactly what 
those two methods I showed above do.

This isn't an unusual design. Java's Swing table component works the 
same way, as did the ones in the old MacApp and PowerPlant frameworks.

—Jens

Related mailsAuthorDate
mlGuidance for Cocoa's steep learning curve Erik Buck May 15, 03:19
mlRe: Guidance for Cocoa's steep learning curve Ricky Sharp May 15, 04:26
mlRe: Guidance for Cocoa's steep learning curve Scott Ribe May 15, 05:33
mlRe: Guidance for Cocoa's steep learning curve David Wilson May 15, 07:16
mlRe: Guidance for Cocoa's steep learning curve Jens Alfke May 15, 16:59
mlRe: Guidance for Cocoa's steep learning curve Scott Ribe May 15, 17:18
mlRe: Guidance for Cocoa's steep learning curve colo May 15, 17:21
mlRe: Guidance for Cocoa's steep learning curve Uli Kusterer May 15, 17:54
mlRe: Guidance for Cocoa's steep learning curve colo May 15, 18:03
mlRe: Guidance for Cocoa's steep learning curve Jens Alfke May 15, 21:06
mlRe: Guidance for Cocoa's steep learning curve Stefan Werner May 15, 21:17
mlRe: Guidance for Cocoa's steep learning curve Scott Ribe May 15, 21:53
mlRe: Guidance for Cocoa's steep learning curve Bruno Sanz Marino May 16, 00:39
mlRe: Guidance for Cocoa's steep learning curve mmalc crawford May 16, 02:03
mlRe: Guidance for Cocoa's steep learning curve Jens Alfke May 16, 03:06
mlRe: Guidance for Cocoa's steep learning curve Joseph Ayers May 16, 03:33
mlRe: Guidance for Cocoa's steep learning curve Jens Alfke May 16, 04:07
mlRe: Guidance for Cocoa's steep learning curve mmalc crawford May 16, 06:12
mlRe: Guidance for Cocoa's steep learning curve James Merkel May 16, 07:04
mlRe: Guidance for Cocoa's steep learning curve John Terranova May 16, 07:32
mlRe: Guidance for Cocoa's steep learning curve Ilan Volow May 16, 08:55
mlRE: Guidance for Cocoa's steep learning curve john darnell May 16, 15:30
mlRe: Guidance for Cocoa's steep learning curve Michael Ash May 16, 15:49
mlRe: Guidance for Cocoa's steep learning curve I. Savant May 16, 15:57
mlRE: Guidance for Cocoa's steep learning curve john darnell May 16, 16:19
mlRe: Guidance for Cocoa's steep learning curve I. Savant May 16, 16:27
mlRe: Guidance for Cocoa's steep learning curve Jeff LaMarche May 16, 16:29
mlRe: Guidance for Cocoa's steep learning curve Michael Ash May 16, 16:31
mlRe: Guidance for Cocoa's steep learning curve Jens Alfke May 16, 16:50
mlRE: Guidance for Cocoa's steep learning curve john darnell May 16, 16:57
mlRe: Guidance for Cocoa's steep learning curve I. Savant May 16, 17:04
mlRe: Guidance for Cocoa's steep learning curve Michael Vannorsdel May 16, 17:41
mlRe: Guidance for Cocoa's steep learning curve Andy Lee May 16, 20:05
mlRe: Guidance for Cocoa's steep learning curve Scott Ribe May 16, 20:22
mlRe: Guidance for Cocoa's steep learning curve Shawn Erickson May 16, 21:51
mlRe: Guidance for Cocoa's steep learning curve Michael Ash May 17, 08:56
mlRe: Guidance for Cocoa's steep learning curve Torsten Curdt May 17, 11:53
mlRe: Guidance for Cocoa's steep learning curve Michael Ash May 17, 12:02
mlRe: Guidance for Cocoa's steep learning curve Torsten Curdt May 17, 12:46
mlRE: Guidance for Cocoa's steep learning curve john darnell May 19, 15:26
mlRe: Guidance for Cocoa's steep learning curve David Casseres May 22, 05:59
mlRe: Guidance for Cocoa's steep learning curve David Casseres May 22, 06:12