问题
Say I have a NSTableView (for example, a) and a NSOutlineView (for example, b).
In the datasource of a I'm trying to get the parent item of all the selected items of b. In the row no. rowIndex of a I would like to have the rowIndexth selected item of b and I would like to concatenate this string to the name of the parent of this item. For example, a:
- 1 and 1.1
- 2
And b:
- 1
- 1.1 (selected)
- 2 (selected)
- 3
This is - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex of 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;
The problem is that when I expand an item [outlineView parentForItem:[outlineView itemAtRow:index]] returns always the last expanded item.
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.
回答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.
来源:https://stackoverflow.com/questions/4984436/nsoutlineview-row-indexes-changes-when-expanding-item