0
votes

I'm a Cocoa Newbie. I have a cocoa application that contains an NSTableView with a text column, and a preview NSTextView field that show the selected row's text.

  1. I'm not using bindings
  2. The delegate and dataSource are properly set
  3. The table uses NSMutableArray of MyObject's

Now, i can add or delete rows successfully. I can change the selection and the corresponding selected text appears in the UITextView.

Problem: As soon as i use NSTextView to change/edit the value of any of the row/column, the NSTableView disturbs my NSMutableArray and shows incorrect data. After that, changing selection shows different states in the table view. See attached screen-shots.

State 1: (Showing populated data)

State 1

State 2: (Row selection changed)

State 2

State 3: (Row selection changed)

State 3

State 4: (About to Edit the Value!)

State 4

State 5: (Highlight/Select Row # 1)

State 5

State 6: (Highlight/Select Row # 2)

State 6

I'm using following method to get the updated value from UITextView:

- (void)textDidEndEditing:(NSNotification *)notification {
    id sender = [notification object];
    NSString * theString = [sender string];

    NSInteger selectedRow = [_myTableView selectedRow];

    if (selectedRow < 0 || selectedRow > [_myArray count] - 1) return;

    MyObject *rowObject = [_myArray objectAtIndex:selectedRow];
    rowObject.text = theString;

    // Update table
    [_myTableView reloadData];
}

I've tried to implement a workaround for apparent reload bug in NSTableView, but this doesn't work for me:

- (void)tableViewSelectionIsChanging:(NSNotification *)notification {

    if ([notification object] == _myTableView) {
        [_myTableView noteNumberOfRowsChanged];
    }
}

I appreciate any help in identifying the source of the problem.

1

1 Answers

0
votes

After wasting countless hours, i was finally able to fix the problem. The modification made to textDidEndEditing: method is as under:

- (void)textDidEndEditing:(NSNotification *)notification {
    id sender = [notification object];
    NSString * theString = [sender string];

    NSInteger selectedRow = [_myTableView selectedRow];

    if (selectedRow < 0 || selectedRow > [_myArray count] - 1) return;

    MyObject *rowObject = [_myArray objectAtIndex:selectedRow];

    // Problematic!
    // rowObject.text = theString;

    // Working!
    rowObject.text = [NSString stringWithString:theString];

    // Update table
    [_myTableView reloadData];
}