I'm trying to select and edit the name property of a newly added object.
On OSX, I have an array controller which displays its content in a table view. I have the table column's values connected (using bindings) through the AC's properties. Also, I set the table view's content and selectionIndexes bindings to point to the AC.
My subclassed AC has an IBOutlet to the table view (called tableView), and contains managed objects from a data model.
In my AC, I override the add: method.
- (void)add:(id)sender {
[super add:sender];
[[self managedObjectContext] processPendingChanges]; // no effect
[tableView reloadData]; // no effect
[tableView scrollRowToVisible:[[self arrangedObjects] count]-1];
}
The newly added object appears in the table view in a selected state (I have the AC set up in IB to select newly inserted objects). But the table view scrolls down to the second last row, putting the new row just outside the visible view.
When I try this,
[tableView scrollRowToVisible:[tableView selectedRow]];
or this,
[tableView scrollRowToVisible:[self selectionIndex]];
it gets even worse: the selectionIndex does not seem to be updated correctly.
- (void)add:(id)sender {
[super add:sender];
NSLog(@"selectionIndex = %lu", [self selectionIndex]);
NSLog(@"number of objects in AC = %lu", [[self arrangedObjects] count]);
}
Logging the selectionIndex reveals that it always shows the previous selection index. Logging the number of objects in the AC is always one too little.
Am I trying to manipulate the table view too early? Any ideas which method would be better suited to override?
About the editing part..
The following statement interferes with the above scrollRowToVisible: method, as the last argument seems to select the row as well.
[tableView editColumn:0 row:0 withEvent:nil select:YES];
Anyway, the specified field (for testing purposes the first row of the table) seems to go into editing mode for just a flicker of an instant, but then finishes editing immediately.
Any help would be greatly appreciated.