3
votes

For a NSTableView that is bound to an arraycontroller through a list i was trying to get the value of a cell of the selected row. The code is -

int row = [minMaxTableView selectedRow];
NSTableColumn *column = [minMaxTableView
                                         tableColumnWithIdentifier:@"min_value"];
NSCell *cell = [column dataCellForRow:row];
NSLog(@"min is --%@",[cell stringValue]);

where minMaxTableView is my table view and min_value is the column identifier. But I am not able to get the exact value of the cell. My requirement is like-- If I modify the edited column value to say "-asnf", I should get the value '-asnf' as the output of min.I wrote this code in delegate

-(void)controlTextDidEndEditing:(NSNotification *)obj
3
is this cell based or view based?Anoop Vaidya
This is a cell based.triandicAnt

3 Answers

2
votes

You can get those row and column by this delegate method. And here you set the new value for the tableview datasource aswell.

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)value forTableColumn:(NSTableColumn *)column row:(NSInteger)row {          
    [states replaceObjectAtIndex:row withObject:value];
    NSLog(@"You changed %@ for row:%ld, column with identifier:%@",value, row,[column identifier]);
    [tableView reloadData];
}

For array controller on

- (void)tableViewSelectionDidChange:(NSNotification *)notification;{

    NSLog(@"here %ld, %@", [[notification object] selectedRow], [self.arrayController arrangedObjects][[[notification object] selectedRow]]);
}
1
votes

There is an easy way without subclassing NSTableView and overriding mouseDown. In willDisplayCell get the selectedRow and editedColumn:

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if(tableView == self.tableView)
    {
        if (row == [tableView editedRow] && [[tableView tableColumns] indexOfObject:tableColumn] == [tableView editedColumn])
            {
             NSLog(@"cell string value is %@",[cell stringValue]);
            }
    }
}
0
votes

You can subclass NSTableView and override mouseDown event to get the selected row index and column index:

-(void)mouseDown:(NSEvent *)event
{
    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];  //Here getting the location of the mouse clicked pont.
    long column = [self columnAtPoint:point];   //Here getting the selected column index.

    long row = [self rowAtPoint:point];     //Here getting the selected row index.
    NSTableColumn* aColumn = [[self tableColumns] objectAtIndex:column];    //Here getting the selected Table Column

    NSCell *aCell = [aColumn dataCellForRow:row];   //Here getting the exactly selected cell

    //Here setting the rspective properties to access them in a controller file.
    [self setSelectedCellRowIndex:[NSNumber numberWithLong:row]];   //Selected Row Index

    [self setSelectedCellColumnIndex:[NSNumber numberWithLong:column]]; //Selected Column Index
    [self setColumnSelected:aColumn];   //Selected Table column
    [self setCustomSelectedCell:aCell]; //Selected Cell

    [super mouseDown:event]; //Here calling the super mouseDown event
}

Now in your controller file, in table view's delegate willDisplayCell get the cell value:

- (void)tableView:(NSTableView *)inTableView willDisplayCell:(id)inCell forTableColumn:(NSTableColumn *)inTableColumn  row:(NSInteger)inRow
{
 if((inRow ==[myTableView selectedRow]) && inTableColumn == [myTableView columnSelected]) {

NSLog(@"My selected cell %@",[incell stringValue]);// string value of your selected cell
}
}