How do I edit a row in the NSTableView to allow the data in that row to be deleted and replaced with new data?
I am creating 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 doing what I want to do, but I haven't quit. Here is my code:
/*
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 source to share
It's not clear what your problem is, so it's better to implement editing here:
Why isn't your data sourcetableView:setObjectValue:forTableColumn:row:
easy to reply to messages ? The user can then edit the values directly in the table view by double-clicking them; no need for a separate text box.
There's also a delegate method that you can implement if you only want to allow editing of some columns and not others.
a source to share
Peter's answer is correct, but just in case someone would be looking for a complete method to edit a string:
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
YourClassWhichHoldsRowRecord *abc = [yourMutableArray objectAtIndex:row];
[abc setValue:object forKey: [tableColumn identifier]];
}
a source to share