0

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

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

  • 1 和 1.1
  • 2

并且b

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

这是- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndexa

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]]总是返回最后一个展开的项目。

I am trying to have a list of selected items and their parents. When I expand an item, all items change their parent to last expanded item.

Am I doing something wrong?

Thank you in advance.

4

1 回答 1

1

You can't use a simple integer to represent an index in a nested set of data, as you found out. Instead you need to use NSIndexPath. As the docs put it:

The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path.

于 2011-04-05T14:47:33.450 回答