I currently have a tableview, with cell views which have NSTextFields inside of them. Currently, on row select, I'm sending the cell view the following message with the hope of granting the NSTextView in the cell view first responder status:
- (void)notifyOfSelectionInWindow:(NSWindow *)window {
[[self textField] setEditable:YES];
[[self textField] setSelectable:YES];
// make textField (textView) first responder
[[self textField] selectText:nil];
[[[self textField] currentEditor] setSelectedRange:NSMakeRange([[[self textField] stringValue] length], 0)];
}
Because I don't want the NSTextFields to be editable when the row they are in isn't selected, I also do this in my custom NSTextField subclass:
- (void)textDidEndEditing:(NSNotification *)notification {
[self setEditable:NO];
[self setSelectable:NO];
[super textDidEndEditing:notification];
}
Selection update code: (note that I'm also changing row heights here)
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row {
// get the table veiw to animate/recalculate height of row
NSMutableIndexSet *changedRows = [NSMutableIndexSet indexSet];
[changedRows addIndex:row];
[changedRows addIndex:[tableView selectedRow]];
[tableView noteHeightOfRowsWithIndexesChanged:changedRows];
[rowView notifyOfSelectionInWindow:[self window]];
// ^ this in turn calls a method of the same name of the corresponding cell view
return YES;
}
Problem is, this works only half of the time. The first time I try selecting a row, first responder status returns to the table view. The second time, it works perfectly, and the Text field has focus. The third time, it breaks again. The fourth - it's perfect! For some strange reason, the code only works every other time...
Anyone have any idea why this is so? Any enlightening feedback is much appreciated.