Model
You need to choose a model, that is, how you are going to represent the data that appears in the table view. For instance:
// SomeObject.h #import <Foundation/Foundation.h> @interface SomeObject @property (copy) NSString *name; @property (assign,getter=isVisible) BOOL visible; @property (assign,getter=isOpaque) BOOL opaque; @property (assign,getter=isAnimatable) BOOL animatable; @end // SomeObject.m #import "SomeObject.h" @implementation SomeObject @synthesize name, visible, opaque, animatable; - (void)dealloc { [name release]; [super dealloc]; } @end
Nib file
For this answer, give the identifiers of the table columns that match the property names in SomeObject
.
Providing values โโfrom a model as a table
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row { // Retrieve the model object corresponding to `row' SomeObject *obj = [myArray objectAtIndex:row]; // Return the object property corresponding to the column if([[tableColumn identifier] isEqualToString:@"name"]) { return obj.name; } // Since this method has return type `id', we need to box the // boolean values inside an `NSNumber' instance else if([[tableColumn identifier] isEqualToString:@"visible"]) { return [NSNumber numberWithBool:obj.visible]; } else if([[tableColumn identifier] isEqualToString:@"opaque"]) { return [NSNumber numberWithBool:obj.opaque]; } else if([[tableColumn identifier] isEqualToString:@"animatable"]) { return [NSNumber numberWithBool:obj.animatable]; } return nil; }
Using values โโfrom a table view to update a model
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { // Retrieve the model object corresponding to `row' SomeObject *obj = [myArray objectAtIndex:row]; // Set the object property corresponding to the column if([[tableColumn identifier] isEqualToString:@"name"]) { obj.name = anObject; } // Since the new value (`anObject') is an object, we need to // convert it to `BOOL' by sending it `-boolValue' else if([[tableColumn identifier] isEqualToString:@"visible"]) { obj.visible = [anObject boolValue]; } else if([[tableColumn identifier] isEqualToString:@"opaque"]) { obj.opaque = [anObject boolValue]; } else if([[tableColumn identifier] isEqualToString:@"animatable"]) { obj.animatable = [anObject boolValue]; } }
You can make this code simpler by using keyword coding, but this is left as an exercise after you study the data sources of the table .: P
user557219
source share