NSOutlineView 单元格具有相同值时出现问题
我有一个 Mac OS X 应用程序,它使用具有两列的 NSOutlineView:键和值,您可以在其中编辑值列。我要么有一个 NSString 要么有一个 NSDictionary 连续。单元格值的代码如下:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([[[tableColumn headerCell] stringValue] isEqualToString:@"Key"]) {
id parentItem = [outlineView parentForItem:item] ? [outlineView parentForItem:item] : root;
return [[parentItem allKeysForObject:item] objectAtIndex:0];
} else {
if ([item isKindOfClass:[NSString class]]) {
return item;
} else if ([item isKindOfClass:[NSDictionary class]]) {
return @"";
} else {
return nil;
}
}
}
它正常工作,除了何时值字段具有相同的字符串值。它总是只将具有该值的第一个元素显示为键,因此相同的键值将出现在所有相同的值中。有人知道如何解决这个问题吗?
I have a Mac OS X application that uses an NSOutlineView with two columns: key and value, where you can edit the value column. I either have a NSString or a NSDictionary in a row. The code for the value of the cells is like this:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([[[tableColumn headerCell] stringValue] isEqualToString:@"Key"]) {
id parentItem = [outlineView parentForItem:item] ? [outlineView parentForItem:item] : root;
return [[parentItem allKeysForObject:item] objectAtIndex:0];
} else {
if ([item isKindOfClass:[NSString class]]) {
return item;
} else if ([item isKindOfClass:[NSDictionary class]]) {
return @"";
} else {
return nil;
}
}
}
It's working as it should, except for when to value fields has the same string value. It always just takes the first element with that value to show as the key, so the same key value will appear for all value values that are the same. Anybody know how to fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在显示一棵字典树,其对象是字符串或字典。
第一个问题是每个项目对象必须唯一标识一行。键和值都不具有此属性。 (如果这是一个平面表格视图,则键是,但这是一个大纲视图,并且两个字典(一个是另一个的后代、兄弟或表兄弟)可以具有相同的键。)相反,您应该创建一个模型对象对于每个键值对。
其次,字典值行应该是组项。您可以为此实现一个委托方法。
It looks like you're showing a tree of dictionaries, whose objects are either strings or dictionaries.
The first problem is that every item object must uniquely identify a row. Neither the key nor the value has this property. (The key would if this were a flat table view, but this is an outline view, and two dictionaries—one a descendant, sibling, or cousin of the other—can have the same key.) Instead, you should make a model object for each key-value pair.
Second, the dictionary-value rows should be group items. There's a delegate method you can implement for this.