1

NSOutlineView 出现了一个奇怪的问题。该视图本质上是一个应用程序列表,其中包含关联文件作为子项。我在它的数据源中手动填充视图,所有这些都很好。我现在想要做的是有一个按钮来删除一个项目。为了做到这一点,我实现了一个方法 removeAppOrFile,如下所示:

- (IBAction)removeAppOrFile:(id)sender
{
    NSInteger selectedRow = [myView selectedRow];
    if (selectedRow == -1) //ie. nothing's selected
    {
        return;
    }  
    NSTableColumn *col = [myView tableColumnWithIdentifier:@"Column 1"];
    NSCell *cell = [col dataCellForRow:selectedRow];
    NSString *item = [cell stringValue];
    NSLog(@"The row is: %ld\nThe column is: %@\nThe cell is: %@\nThe selected item is: %@",selectedRow, col, cell, item); // For testing purposes
}

myView 是一个连接到我的 NSOutlineView 的 IBOutlet。如果我选择不同的行并单击按钮,则 selectedRow 的值将正确更改,但 NSCell 对象永远不会改变,它的值应该是什么(即 NSString 项)总是显示最后一个可见项的值(即,如果有一个以子项作为最后一项的项 NSString 项将是父项,如果它没有展开,或者最后一个子项,如果它被展开)。

奇怪的是,我在其他地方使用基本相同的代码作为 NSOutlineView 上的 doubleAction 并且它运行良好。在这种情况下,代码如下:

- (void)editedAppOrFile:(id)sender 
{
    NSInteger rowNumber = [sender clickedRow];
    NSTableColumn *col = [sender tableColumnWithIdentifier:@"Column 1"];
    NSCell *cell = [col dataCellForRow:rowNumber];
    NSString *item = [cell stringValue];
    NSLog(@"The row is: %ld\nThe column is: %@\nThe cell is: %@\nThe selected item is: %@",selectedRow, col, cell, item); // For testing purposes
}

在这种情况下,发送者是大纲视图。项目和单元格随 rowNumber 变化而变化。

关于为什么它在第一个示例中不起作用的任何想法?

4

1 回答 1

3

您的方法存在一些问题。

  1. 你得到的是数据单元,而不是-preparedCellAtColumn:row:,所以你不能保证它的内部对象值是什么。
  2. 您可以直接询问大纲视图-itemAtRow:
  3. 如果您尝试删除(在第一种情况下)或编辑(第二种情况下),您实际上只需要修改您的数据源,然后记下更改的行数(第一种情况)或重新加载行数据(第二种情况) .
于 2011-08-19T15:46:54.740 回答