开发者

NSTableView update/reload problem

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开发者_C百科 selection shows different states in the table view. See attached screen-shots.

State 1: (Showing populated data)

NSTableView update/reload problem

State 2: (Row selection changed)

NSTableView update/reload problem

State 3: (Row selection changed)

NSTableView update/reload problem

State 4: (About to Edit the Value!)

NSTableView update/reload problem

State 5: (Highlight/Select Row # 1)

NSTableView update/reload problem

State 6: (Highlight/Select Row # 2)

NSTableView update/reload problem

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.


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜