【问题标题】:NSOutlineView row indexes changes when expanding itemNSOutlineView 行索引在扩展项目时发生变化
【发布时间】:2011-02-13 13:36:28
【问题描述】:

假设我有一个 NSTableView(例如,a)和一个 NSOutlineView(例如,b)。

a 的数据源中,我试图获取b 的所有选定项的父项。在行号中。 rowIndex of a 我想要rowIndexth 选中的b 的项目并且我想将此字符串连接到该项目的父项的名称。例如a:

  • 1 和 1.1
  • 2

还有b

  • 1
    • 1.1(已选择)
  • 2(已选择)
  • 3

这是- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex 中的a

NSUInteger index = [[outlineView selectedRowIndexes] firstIndex];

for (NSUInteger i = 0, target = rowIndex; i < target; i++)
    index = [[outlineView selectedRowIndexes] indexGreaterThanIndex:index];

NSLog(@"%@", [outlineView [outlineView itemAtRow:index]]);

if([outlineView parentForItem:[outlineView itemAtRow:index]] != NULL) {
    return [NSString stringWithFormat:@"%@ %@", [outlineView parentForItem:[outlineView itemAtRow:index]], [outlineView itemAtRow:index]]; 
} else {
    return [outlineView itemAtRow:index];
}

return NULL;

问题是当我展开一个项目时[outlineView parentForItem:[outlineView itemAtRow:index]] 总是返回最后一个展开的项目。

我正在尝试列出所选项目及其父项的列表。当我展开一个项目时,所有项目都将其父级更改为last expanded item

我做错了吗?

提前谢谢你。

【问题讨论】:

    标签: cocoa nsoutlineview


    【解决方案1】:

    正如您所发现的,您不能使用简单的整数来表示嵌套数据集中的索引。相反,您需要使用NSIndexPath。正如文档所说:

    NSIndexPath 类表示 树中特定节点的路径 嵌套数组集合。这条路是 称为索引路径。

    【讨论】:

    • 谢谢。这就是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多