The purpose of my code is to have an NSTextField, button, and an NSTableView. When the button is pressed a new cell is created in the table and the data from the textfield is added into the cell.
#import <Cocoa/Cocoa.h>
#import "ItemData.h"
@interface AppController : NSObject {
IBOutlet NSTableView *myTable; // Outlet to the table view, connected in IB.
NSMutableArray *items; // An array of AppointmentData objects.
}
- (IBAction)addItem

id)sender;
- (IBAction)removeSelectedItem

id)sender;
@end
#import "AppController.h"
@implementation AppController
- (void)dealloc {
[items release];
items = nil;
[super dealloc];
}
- (void)awakeFromNib {
// Create the storage our table will use.
items = [[NSMutableArray alloc] init];
// Be the data source...
[myTable setDataSource:self];
// Let the text in the @"Info" column wrap.
[[[myTable tableColumnWithIdentifier

"Info"] dataCell] setWraps:YES];
// Start with atleast one item.
[self addItem:nil];
}
// ---------------------------------------------------------
// Action methods
// ---------------------------------------------------------
- (void)addItem

id)sender {
// Append a newly created data object, then reload the table contents.
ItemData *itemData = [[ItemData alloc] init];
[items addObject: itemData];
[itemData release];
[myTable reloadData];
}
- (void)removeSelectedItem

id)sender {
// Remove the selected row from the data set, then reload the table contents.
[items removeObjectAtIndex: [myTable selectedRow]];
[myTable reloadData];
}
// ---------------------------------------------------------
// Data source methods
// ---------------------------------------------------------
- (int)numberOfRowsInTableView

NSTableView *)tv {
return [items count];
}
- (id)tableView

NSTableView *)tv objectValueForTableColumn

NSTableColumn *)tc row

int)row {
return [[items objectAtIndex:row] info];
}
- (void)tableView

NSTableView *)tv setObjectValue

id)objectValue forTableColumn

NSTableColumn *)tc row

int)row {
[[items objectAtIndex:row] setInfo: objectValue];
}
@end
#import <Foundation/Foundation.h>
@interface ItemData : NSObject {
IBOutlet NSTextField *newDataField;
NSString *info;
NSString *newData;
}
- (void)setInfo

NSString *)newInfo;
- (NSString *)info;
@end
#import "ItemData.h"
@implementation ItemData
- (id)init {
self = [super init];
if (self) {
NSString *newData = @"%@", newDataField;
[self setInfo: newData];
}
return self;
}
- (void)dealloc {
[info release];
info = nil;
[super dealloc];
}
- (void)setInfo

NSString *)newInfo {
if (info != newInfo) {
[info release];
info = [newInfo copy];
}
}
- (NSString *)info {
return [[info retain] autorelease];
}
@end