views:

87

answers:

1

I'm building a To-Do-List application which uses the following objects:

  1. NSTexField for data entry
  2. NSTableView to display the data (just string objects)
  3. 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.

  1. wash the car
  2. buy Starcraft II
  3. 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;

}
A: 

First off, the default for -tableView:shouldEditTableColumn:row: is YES so you don't need to implement it, although you may later choose to restrict editing of some rows/columns, so it's fine to leave it there.

Apart from what you have done, you are missing an implementation of -tableView:setObjectValue:forTableColumn:row:. If you implement that and replace the item in your array with the object value you are given, everything should be fine. You won't even need to reload the data.

Edit: What I have described allows people to edit things in place in the table view. Your button is then only needed to add new items.

JeremyP
So I need to add another implementation tableView:setObjectValue:forTableColumn:row in addition to the one I already have?
lampShade
My mistake the method you mentioned is different.
lampShade