Hello. I'm trying to learn Cocoa, and I thought for my first application I'd try to make a simple recipe manager. I ran into some issues and along the way and in trying to figure it out I ran into some of the excellent tutorials here at cocoadev.com. That said, I'm still having issues. Basically I'm wanting to implement a multi-column table view that the user clicks a button to add a new row and then can edit. Currently I've got it where the table view loads a blank row that I can double click and change, but it will not save any changes made. Here's the code of the controller:
AppController.m:
I'm sure I'm missing something simple, but I would appreciate any guidance and help you could give.
AppController.m:
Code:
- (void)awakeFromNib
{
//create our original array
items = [NSMutableArray new];
}
- (id)selectedRowItemforColumnIdentifier:(NSString *)anIdentifier {
if ([ingredientTable selectedRow] != -1)
return [[items objectAtIndex:[ingredientTable selectedRow]] objectForKey:anIdentifier];
return nil;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
if (row != -1)
return [[items objectAtIndex:row] objectForKey:[tableColumn identifier]];
return nil;
}
- (void)setItems:(NSMutableArray *)anArray {
if (items == anArray)
return;
[items release];
items = anArray;
[items retain];
[ingredientTable reloadData];
}
- (void)addRow:(NSDictionary *)item {
[items insertObject:item atIndex:[items count]];
[ingredientTable reloadData];
}
- (void)removeRow:(unsigned)row {
[items removeObjectAtIndex:row];
[ingredientTable reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView {
return [items count];
}
//occurs when the add row button is clicked
- (IBAction)add:(id)sender
{
NSMutableDictionary *dict;
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"", @"Quantity",
@"", @"Ingredient", nil];
[self addRow:dict];
}
//occurs when the remove row button is clicked
- (IBAction)remove:(id)sender
{
if ([ingredientTable selectedRow] < 0 || [ingredientTable selectedRow] >= [items count])
return;
[items removeObjectAtIndex:[ingredientTable selectedRow]];
[ingredientTable reloadData];
}
I'm sure I'm missing something simple, but I would appreciate any guidance and help you could give.