从 NSOutlineView 检索单元格内容
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法存在一些问题。
-preparedCellAtColumn:row:
,因此您无法保证其内部对象值是什么。-itemAtRow:
。There are a few issues with your approach.
-preparedCellAtColumn:row:
, so you have no guarantees about what its internal object value will be.-itemAtRow:
.