Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get elements inside a selected row in a NSTableView?

I have an NSTableView of 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 a selected row, I want to get an element inside the selected row for a specific column. Specifically, I want to get the Text Field in the first column for the selected row.

I have an outlet to the table view and I have the index of the selected row so far, but that's about it:

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

Any ideas on how I can go about tackling this problem?

Edit: This is what I am trying to do: I want to change the text field to be in an editing state and focus on it so the user can start editing the text field value as soon as it is selected

like image 731
Aamir Avatar asked Oct 11 '12 18:10

Aamir


People also ask

How do I select a row and column in a table?

Use the SelectedRows property. To enable users to select rows, you must set the SelectionMode property to FullRowSelect or RowHeaderSelect. Use the SelectedColumns property. To enable users to select columns, you must set the SelectionMode property to FullColumnSelect or ColumnHeaderSelect.

How to get selected cells and rows from a datagridview?

You can get the selected cells, rows, or columns from a DataGridView control by using the corresponding properties: SelectedCells, SelectedRows, and SelectedColumns. In the following procedures, you will get the selected cells and display their row and column indexes in a MessageBox. Use the SelectedCells property.

How to check if a row has been selected from a column?

first you shall specify a variable (e.g:flg_sel) for table control attribute "w/SelColumn" . second ,you shall judge whether flg_sel = 'X' in you source code ,when flg_sel ='X' ,indicate that row was selected .

How to access table row element in JavaScript?

Approach: We will use a basic DOM operation in JavaScript to access table row element. We will be adding highlight class to the row that we click, if the highlight class is already present then we will remove this class to make it normal.


1 Answers

I seemed to have solved my own problem:

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

    // Get row at specified index
    NSTableCellView *selectedRow = [tableView viewAtColumn:0 row:selected makeIfNecessary:YES];

    // Get row's 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)];
}
like image 158
Aamir Avatar answered Oct 11 '22 11:10

Aamir