tags:

views:

12

answers:

1

I'm trying to access the array (data source) in my NSTableView to allow replacing the string that is in the row with a new string.The app consist of an NSTextfield for data entry and a button to add the entry so that it's displayed in the NSTableView. What I want is to be able to double click the NSTableView and replace what ever string is there with a new string, but I'm not sure how to do this.Here is my code so far

@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
{
    NSString *newString = [[NSString alloc]init];
    NSLog(@"The selected row %d",[tableView selectedRow]);
    newString = [textField stringValue];
    [array addObject:newString];
    [array replaceObjectAtIndex:[tableView selectedRow ]  withObject: newString];
    NSLog(@"this is the new sting %@",newString);
    [tableView reloadData];

}


@end
+1  A: 

I think you're looking for these datasource and delegate methods:

kperryua