Hey guys, newb here.
I've been working my way through Aaron Hillegass' "Cocoa Programming for Mac OS X" book and I decided to try his challenge in Chapter 6: Make a Data Source. The program works fine, but I decided to try adding the bonus criteria he mentioned. It says to make the rows editable and provides a hint that NSMutableArray has a method called replaceObjectAtIndex:withObject:. This has me a bit stumped because I'm not sure how to incorporate this into the existing application. The NSTableView is set to editable, so if I double-click on a row I can type on it. Those changes aren't saved though since I don't have the proper code behind it.
This is how my application looks right now (without the code relating to editing the rows):
Now I was expecting to have to add another method, something like - (IBAction)editIt
id)sender but I'm not sure what to bind this to on the NSTableView. Is that even necessary or is there something else I need to be doing? I thought maybe something like this:
But this has no effect. When I double-click a row and change the text, I hit enter and it goes back to what the value was originally. I know this is a bigtime newb question but I appreciate your help.
This is how my application looks right now (without the code relating to editing the rows):
Code:
//
// AppController.m
// DataSourceChallenge
//
#import "AppController.h"
@implementation AppController
- (id)init
{
[super init];
toDoList = [[NSMutableArray alloc] init];
NSLog(@"init");
return self;
}
- (IBAction)addIt:(id)sender
{
NSString *addedString = [textField stringValue];
if ([addedString length] == 0)
{
NSLog(@"User tried to add a blank line.");
NSRunAlertPanel(@"Error", @"You cannot add a blank line to the to-do list.", @"OK", nil, nil);
return;
}
[toDoList addObject:addedString];
NSLog(@"Added the string: %@", addedString);
[textField setStringValue:@""];
[tableView reloadData];
NSLog(@"Ran reloadData.");
}
- (int)numberOfRowsInTableView:(NSTableView *)tv
{
return [toDoList count];
}
- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
NSString *ti = [toDoList objectAtIndex:row];
return ti;
}
@end
Code:
//
// AppController.h
// DataSourceChallenge
//
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSTextField *textField;
IBOutlet NSButton *addButton;
IBOutlet NSTableView *tableView;
NSMutableArray *toDoList;
}
- (IBAction)addIt:(id)sender;
@end
Now I was expecting to have to add another method, something like - (IBAction)editIt
Code:
- (void)tableView:(NSTableView *)tv replaceObjectAtIndex:(int)indexRow withObject:(NSString *)tempNewString
{
[toDoList replaceObjectAtIndex:indexRow withObject:tempNewString];
[tableView reloadData];
return;
}
But this has no effect. When I double-click a row and change the text, I hit enter and it goes back to what the value was originally. I know this is a bigtime newb question but I appreciate your help.