I wanted to make a table with the datasource as a seperate object from my AppDelegate. It seems like I am creating functions to get/set the data that should already be there? I'm probably just doing it wrong. The program works, it just seems repetitive. Mainly the datasource model - am I missing a way to use the 'tableview' functions to get/set the data rows?
AppDelgate.m
MyDataSource.m
My app looks like this:
Basically you just add/delete rows. If there's anything else I'm doing wrong that I'm not realizing please point it out to me, the more tips/advice the better.
See the attached project for more details.
AppDelgate.m
Code:
//
// AppDelegate.m
// Tables
#import "AppDelegate.h"
@implementation AppDelegate
//@synthesize myTable = _myTable;
@synthesize window = _window;
//@synthesize input;
- (void)dealloc
{
[myTable release];
[myData release];
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[myTable setBackgroundColor:[NSColor grayColor]];
myData = [[MyDataSource alloc] init];
[myTable setDataSource:myData];
[myTable setDelegate:self];
selectedRow = -1;
}
// methods
- (IBAction)addItem:(id)sender {
NSLog(@"Adding %@", [input stringValue]);
[myData addItem:[input stringValue]];
[myTable reloadData];
}
- (IBAction)removeItem:(id)sender {
if (selectedRow > -1) {
NSLog(@"Removing %@", [myData getItemAtColumn:0 row:selectedRow]);
[myData removeItemAt:(int)selectedRow];
[myTable reloadData];
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
selectedRow = [myTable selectedRow];
NSLog(@"Selected row is %ld", selectedRow);
}
@end
MyDataSource.m
Code:
//
// MyDataSource.m
// Tables
#import "MyDataSource.h"
@implementation MyDataSource
- (id)init {
items = [[NSMutableArray alloc] init];
[items addObject:[NSString stringWithString:@"Very first object"]];
[items addObject:[NSString stringWithString:@"Another object"]];
[items addObject:[NSString stringWithString:@"Enough objects for now"]];
return self;
}
- (void)addItem:(id)item {
NSLog(@"addItem: %@", item);
[items addObject:item];
}
- (void)removeItemAt:(int)index {
NSLog(@"removeItem: %d", index);
[items removeObjectAtIndex:index];
}
- (id)getItemAtColumn:(NSInteger)col
row:(NSInteger)row {
return [items objectAtIndex:row];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [items count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row {
return [items objectAtIndex:row];
}
- (void)tableView:(NSTableView *)tableView
setObjectValue:(id)object
forTableColumn:(NSTableColumn *)tableColumn
row:(int)row {
[items addObject:object];
}
- (void)dealloc {
[items release];
[super dealloc];
}
@end
My app looks like this:

Basically you just add/delete rows. If there's anything else I'm doing wrong that I'm not realizing please point it out to me, the more tips/advice the better.
See the attached project for more details.