从 NSOutlineView 检索单元格内容

发布于 2024-11-30 14:11:05 字数 1450 浏览 1 评论 0原文

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
}

在这种情况下,发送者是outlineView。项目&单元格随行号变化而变化。

知道为什么它在第一个示例中不起作用吗?

I have an odd issue cropping up with an NSOutlineView. The view is essentially a list of apps with associated files as children. I populate the view by hand in it's data source and all of that works fine. What I now want to do is have a button to remove an item. In order to do it I implemented a method, removeAppOrFile, like so:

- (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 is an IBOutlet connected to my NSOutlineView.
If I select a different row and click the button the value for selectedRow will change properly, but the NSCell object never changes, and what should be it's value (ie. NSString item) always shows that of the last visible item (ie. if there's an item with children as the last item NSString item will be the parent if it's not expanded, or the last child if it is expanded).

The weird thing is that I use basically the same code elsewhere for a doubleAction on an NSOutlineView and it works perfectly. In that case, the code is as below:

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

In this case sender is the outlineView. item & cell change with rowNumber change.

Any idea as to why it's not working in the first example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-12-07 14:11:05

您的方法存在一些问题。

  1. 您获取的是数据单元格,而不是 -preparedCellAtColumn:row:,因此您无法保证其内部对象值是什么。
  2. 您可以直接向大纲视图询问-itemAtRow:
  3. 如果您尝试删除(第一种情况)或编辑(第二种情况),您实际上只需要修改数据源,然后记下更改的行数(第一种情况)或重新加载行数据(第二种情况) 。

There are a few issues with your approach.

  1. You're getting the data cell, not the -preparedCellAtColumn:row:, so you have no guarantees about what its internal object value will be.
  2. You can ask the outline view directly for -itemAtRow:.
  3. If you're trying to remove (in the first case) or edit (the second case), you really only need to modify your data source and then note number of rows changed (first case) or reload data for row (second case).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文