3

对于通过列表绑定到数组控制器的 NSTableView,我试图获取所选行的单元格的值。代码是 -

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

其中 minMaxTableView 是我的表视图, min_value 是列标识符。但我无法获得单元格的确切值。我的要求是——如果我将编辑后的列值修改为“-asnf”,我应该得到值“-asnf”作为 min 的输出。我在委托中编写了这段代码

-(void)controlTextDidEndEditing:(NSNotification *)obj
4

3 回答 3

2

您可以通过此委托方法获取这些行和列。在这里,您还为 tableview 数据源设置了新值。

- (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];
}

对于阵列控制器

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

    NSLog(@"here %ld, %@", [[notification object] selectedRow], [self.arrayController arrangedObjects][[[notification object] selectedRow]]);
}
于 2013-04-22T09:21:28.453 回答
1

有一种简单的方法,无需子类化NSTableView和覆盖mouseDown。在willDisplayCell获取selectedRoweditedColumn

- (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]);
            }
    }
}
于 2014-11-07T06:30:49.807 回答
0

您可以继承 NSTableView 并覆盖mouseDown事件以获取选定的行索引和列索引:

-(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
}

现在在您的控制器文件中,在表视图的委托中willDisplayCell获取单元格值:

- (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
}
}
于 2014-06-25T05:36:26.900 回答