views:

25

answers:

1

I'm building a to-do-list application and I want to be able to edit the entries in the table and replace them with new entries. I'm close to being able to do what I want but not quit. Here is my code so far:

/*
 IBOutlet NSTextField *textField;
 IBOutlet NSTabView *tableView;
 IBOutlet NSButton *button;
 NSMutableArray *myArray;
 */

#import "AppController.h"


@implementation AppController


-(IBAction)addNewItem:(id)sender
{

    [myArray addObject:[textField stringValue]];
    [tableView reloadData];     
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [myArray count];
}

- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
            row:(int)rowIndex
{

    return [myArray objectAtIndex:rowIndex];
}

- (id)init
{
    [super init];
    myArray = [[NSMutableArray alloc] init];
    return self;
}
-(IBAction)removeItem:(id)sender
{

    NSLog(@"This is the index of the selected row: %d",[tableView selectedRow]);
    NSLog(@"the clicked row is %d",[tableView clickedRow]);
    [myArray replaceObjectAtIndex:[tableView selectedRow] withObject:[textField stringValue]];
    [myArray addObject:[textField stringValue]];
    //[tableView reloadData];
}

@end
A: 

It's not clear what problem you're having, so here's a better way to implement editing instead:

Why not just have your data source respond to tableView:setObjectValue:forTableColumn:row: messages messages? Then the user can edit the values right in the table view by double-clicking them; no need for a separate text field.

There's also a delegate method you can implement if you want to allow only editing some columns and not others.

Peter Hosey