展开项目时NSOutlineView行索引更改 [英] NSOutlineView row indexes changes when expanding item

查看:226
本文介绍了展开项目时NSOutlineView行索引更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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




  • 1和1.1

  • 2



b




  • 1

    • 1.1(已选择)


  • 2(已选择)

  • 3



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

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



我想要一个选定的项目及其父母的列表。当我展开一个项目时,所有项目都将其父项更改为最后扩展项



我做错了什么?



提前感谢。

解决方案

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


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



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.

解决方案

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.

这篇关于展开项目时NSOutlineView行索引更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆