Strange behavior of view based NSTableView with NSTextView
Strange behavior of view based NSTableView with NSTextView
- Subject: Strange behavior of view based NSTableView with NSTextView
- From: Nick <email@hidden>
- Date: Sat, 18 Jul 2015 16:37:07 +0300
Hello
I am trying to debug a problem in NSTableView but i can't figure out what
is the cause.
Basically I have a View-based NSTableView (the view is NSTextView), and I
would like to be able to add and remove rows to the table using buttons,
and also allow the user to edit the textview that is a cell in NSTableView.
The demo app I created is very simple.
After the app starts, there's a view with 6 rows (corresponding to all the
items in self.entries).
The problem is the following: If the user presses the "remove" button, the
first item (@"1") gets removed form the list (this is correct). But when
the user presses the "add" button, and then types into newly added row "7",
the row gets title "6". I cannot understand why this is happening. The
NSTextViewDelegate's "textDidChange:" method gets invoked only once when
the user types "7" and after the method finishes, the "entries" array looks
good: 1,2,3,4,5,6,7. But later, inside "-tableView:viewForTableColumn:row:
" the entries array automatically changes to "1,2,3,4,5,6,6". When I keep
adding new rows, pressing on the "add" button, the values of the rows start
changing randomly. Looks like something is happening under the hood. I do
not have any other code in my demo app apart from what I am pasting in
here. Could you suggest me what am i doing wrong?
Thanks
@interface AppDelegate ()
@property (weak) IBOutlet NSTableView *tableView;
- (IBAction)add:(id)sender;
- (IBAction)remove:(id)sender;
@property NSMutableArray *entries;
@end
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.entries = [NSMutableArray array];
[self.entries addObjectsFromArray:@[@"1", @"2", @"3", @"4", @"5", @"6"]
];
[self.tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.entries.count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(
NSTableColumn *)tableColumn row:(NSInteger)row {
if([[tableColumn identifier] isEqualToString:@"FirstColumn"]) {
NSTableCellView *tableCellView = [tableView makeViewWithIdentifier:
@"FirstColumnCellView" owner:tableView];
NSTextView *view = [[[tableCellView subviews][0] subviews][0]
subviews][0];
view.string = [NSString stringWithString:self.entries[row]];
view.delegate = self;
return tableCellView;
}
return nil;
}
- (void)textDidChange:(NSNotification *)notification {
NSLog(@"changed");
NSTextView *textView = [notification object];
NSInteger row = [self.tableView rowForView:textView];
self.entries[row] = textView.string;
[self.tableView reloadData];
}
- (IBAction)add:(id)sender {
[self.entries addObject:@""];
[self.tableView reloadData];
}
- (IBAction)remove:(id)sender {
[self.entries removeObjectAtIndex:0];
[self.tableView reloadData];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden