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.
- I'm not using bindings
- The delegate and dataSource are properly set
- 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)
State 2: (Row selection changed)
State 3: (Row selection changed)
State 4: (About to Edit the Value!)
State 5: (Highlight/Select Row # 1)
State 6: (Highlight/Select Row # 2)
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];
}
精彩评论