Reputation: 4391
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
Upvotes: 2
Views: 2290
Reputation: 593
Peter's answer is correct, but just in case someone would be looking for complete method for editing row:
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
YourClassWhichHoldsRowRecord *abc = [yourMutableArray objectAtIndex:row];
[abc setValue:object forKey: [tableColumn identifier]];
}
Upvotes: 0
Reputation: 96333
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.
Upvotes: 1