How to get items inside selected row in NSTableView?

I have an NSTableView content type View Based with two columns.

My objects are structured as follows:

 Table View Table Column Table Cell View Text Field Table Column Table Cell View Popup Button 

For the selected row, I want to get the element inside the selected row for a specific column. In particular, I want to get the Text Field in the first column for the selected row.

I have access to the table view, and so far I have the index of the selected row, but more about that:

 - (void)getSelectedTextField { NSInteger selected = [tableView selectedRow]; } 

Any ideas on how I can solve this problem?

Change This is what I am trying to do: I want to change the text field to be able to edit, and focus on it so that the user can start editing the value of the text field as soon as possible as selected

+6
source share
3 answers

I seem to have solved my problem:

 - (void)getSelectedTextField { NSInteger selected = [tableView selectedRow]; // Get row at specified index NSTableCellView *selectedRow = [tableView viewAtColumn:0 row:selected makeIfNecessary:YES]; // Get row text field NSTextField *selectedRowTextField = [selectedRow textField]; // Focus on text field to make it auto-editable [[self window] makeFirstResponder:selectedRowTextField]; // Set the keyboard carat to the beginning of the text field [[selectedRowTextField currentEditor] setSelectedRange:NSMakeRange(0, 0)]; } 
+18
source

Usage can get the whole table element of a table view, by getting its subview. Suggest a subheading of the logbook list before using it

 NSInteger index = [self.tableViewFiles selectedRow]; // Get row at specified index of column 0 ( We just have 1 column) NSTableCellView *cellView = [self.tableViewFiles viewAtColumn:0 row:index makeIfNecessary:YES]; // Get row checkBox ( at index 0, textFields is 1,2) NSButton *checkBox = [[cellView subviews] objectAtIndex:0]; 
+3
source

If the text field is the only view of your table view cell, you can get a link to it with the following code and select its text so that it is ready for editing:

 -(void)tableViewSelectionDidChange:(NSNotification *)notification { NSInteger row = [notification.object selectedRow]; NSTextField *tf = [[[notification.object viewAtColumn:0 row:row makeIfNecessary:NO]subviews] lastObject]; [tf selectText:tf.stringValue]; } 
0
source

Source: https://habr.com/ru/post/927492/


All Articles