I'm building a To-Do-List application which uses the following objects:
- NSTexField for data entry
- NSTableView to display the data (just string objects)
- NSButton that when clicked will add the contents of the NSTextField to the NSTableView for display.
What I want to do is replace any given string in any row in the NSTableView with another string. For example lets say that the user enters the following.
- wash the car
- buy Starcraft II
- upgrade video card.
But then in the third row want's to change that to - build a new pc. How can I do this? Here is what I have so far:
#import "AppController.h"
@implementation AppController
-(id)init
{
[super init];
[tableView setDataSource:self];
[tableView setDelegate:self];
array = [[NSMutableArray alloc ] init];
//NSLog(@"this is my delegate %@",[tableView delegate]);
return self;
}
-(IBAction)addItem:(id)sender
{
inputString = [textField stringValue];
[array addObject:inputString];
[tableView reloadData];
return;
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [array count];
}
- (id) tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//NSLog(@"this is the object %@",[array objectAtIndex:rowIndex]);
return [array objectAtIndex:rowIndex];
}
-(IBAction) replaceItem:(id)sender
{
NSLog(@"The selected row %d",[tableView selectedRow]);
NSLog(@"The objectAtIndex is: %@",[array objectAtIndex:[tableView selectedRow]]);
[array replaceObjectAtIndex:[tableView selectedRow ] withObject: @"Micheal jordan"];
[tableView reloadData];
return;
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)
aTableColumn row:(NSInteger)rowIndex
{
return YES;
}
@end
ADDENDUM: I added a new method as suggested. It "works" now but not in the way I'd like it to. I wanted to edit the entries in the NSTableView "in line" but I couldn't figure out how to do it.This works now by selecting the row you wish to edit then putting the new string in the NSTextField into the NSTableView.
#import "AppController.h"
@implementation AppController
-(id)init
{
[super init];
[tableView setDataSource:self];
[tableView setDelegate:self];
array = [[NSMutableArray alloc ] init];
//NSLog(@"this is my delegate %@",[tableView delegate]);
return self;
}
-(IBAction)addItem:(id)sender
{
inputString = [textField stringValue];
[array addObject:inputString];
[tableView reloadData];
return;
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [array count];
}
- (id) tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//NSLog(@"this is the object %@",[array objectAtIndex:rowIndex]);
return [array objectAtIndex:rowIndex];
//return [array replaceObjectAtIndex:[tableView selectedRow ] withObject: @"Micheal jordan"];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
[array replaceObjectAtIndex:[tableView selectedRow] withObject:[textField stringValue]];
return;
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)
aTableColumn row:(NSInteger)rowIndex
{
return YES;
}