Unlike most controls, an NSTableView
object does not store or cache the data it displays. Instead, it gets all of its data from an object called a data source that you provide. 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)
If your table supports the editing of records, you must also provide a tableView:setObjectValue:forTableColumn:row:
method for changing the value of an attribute.
The NSTableView
object treats objects provided by its data source as values to be displayed in NSCell
objects. If these objects aren’t of common value classes—such as NSString
, NSNumber
, and so on—you may need to create a custom NSFormatter
object to display them. For more information, see Data Formatting Programming Guide for Cocoa.
The type of information stored in a particular field of a record is indicated by the identifier object of the corresponding column; see “The Parts of a Table.” Because columns can be reordered, you cannot rely on a column's position in the table to identify the type of data it contains. Instead, your data source must use the column’s identifier object as a key to identifying the value. The identifier object can be any kind of object that uniquely identifies attributes for the data source. For example, you could use strings to identify the names of attributes such as “Last Name”, “Address”, and so on. The data source object could then use these strings as keys for NSDictionary
objects.
Suppose that a table view's column identifiers are set up as strings containing the names of attributes for the column, such as “Last Name”, “City”, and so on. Also suppose that the data source stores each record in an NSMutableDictionary
object and that all of the records are then stored in in an NSMutableArray
object called records, Given this configuration, an ASCII property list version of the data might look like the following:
( |
{ |
"Last Name" = Anderson; |
"First Name" = James; |
Abode = apartment; |
City = "San Francisco"; |
}, |
{ |
"Last Name" = Beresford; |
"First Name" = Keith; |
Abode = apartment; |
City = "Redwood City"; |
} |
) |
With such a record structure, the following implementation of the tableView:objectValueForTableColumn:row:
method is sufficient to retrieve values for the NSTableView
:
- (id)tableView:(NSTableView *)aTableView |
objectValueForTableColumn:(NSTableColumn *)aTableColumn |
row:(int)rowIndex |
{ |
id theRecord, theValue; |
NSParameterAssert(rowIndex >= 0 && rowIndex < [records count]); |
theRecord = [records objectAtIndex:rowIndex]; |
theValue = [theRecord objectForKey:[aTableColumn identifier]]; |
return theValue; |
} |
The corresponding method for setting values would look like the following:
- (void)tableView:(NSTableView *)aTableView |
setObjectValue:anObject |
forTableColumn:(NSTableColumn *)aTableColumn |
row:(int)rowIndex |
{ |
id theRecord; |
NSParameterAssert(rowIndex >= 0 && rowIndex < [records count]); |
theRecord = [records objectAtIndex:rowIndex]; |
[theRecord setObject:anObject forKey:[aTableColumn identifier]]; |
return; |
} |
Finally, the numberOfRowsInTableView:
method simply returns the count of the NSArray
object:
- (int)numberOfRowsInTableView:(NSTableView *)aTableView |
{ |
return [records count]; |
} |
In each case, the NSTableView
object that sends the message is passed to the delegate method in the aTableView parameter. A data source object that manages several sets of data can choose the appropriate set based on which NSTableView
object sent the message.
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-04-04)