我有一个NSOutlineView,其中一些有孩子,有些则没有。我想在其他单元格上进行选择时取消选择或关闭之前的选择。
我希望我可以解释这些屏幕截图。
这里我们有两个父文件夹,文件夹1和文件夹2.我想在选择文件夹2的显示三角形时取消选择文件夹1.
使用
这样的东西-(BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item;
-(BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item;
我还没有找到出路。
由于
答案 0 :(得分:1)
实现此委托并使用类似的东西:
- (void)outlineViewItemDidExpand:(NSNotification *)notification{
for (id parent in list) {
if ([notification.userInfo objectForKey:@"NSObject"] == parent) {
continue;
}
[notification.object collapseItem:parent];
}
}
此处parent
是顶级项目数组,如示例Folder1和Folder2。
编辑:
<强> Appdelegate.h 强>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property(strong) NSDictionary *firstParent;
@property(strong) NSDictionary *secondParent;
@property(strong) NSArray *list;
@end
<强> Appdelegate.m 强>
@implementation AppDelegate
@synthesize firstParent;
@synthesize secondParent;
@synthesize list;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (id)init {
self = [super init];
if (self) {
firstParent = [NSDictionary dictionaryWithObjectsAndKeys:@"A",@"parent",
[NSArray arrayWithObjects:@"Apple",@"Aeroplane", nil],@"children", nil];
secondParent = [NSDictionary dictionaryWithObjectsAndKeys:@"B",@"parent",
[NSArray arrayWithObjects:@"Ball",@"Baloon", nil],@"children", nil];
list = [NSArray arrayWithObjects:firstParent,secondParent, nil];
}
return self;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item{
if ([item isKindOfClass:[NSDictionary class]]) {
return YES;
}
else {
return NO;
}
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item{
if (item == nil) { //item is nil when the outline view wants to inquire for root level items
return [list count];
}
if ([item isKindOfClass:[NSDictionary class]]) {
return [[item objectForKey:@"children"] count];
}
return 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{
if (item == nil) { //item is nil when the outline view wants to inquire for root level items
return [list objectAtIndex:index];
}
if ([item isKindOfClass:[NSDictionary class]]) {
return [[item objectForKey:@"children"] objectAtIndex:index];
}
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{
if ([[theColumn identifier] isEqualToString:@"children"]) {
if ([item isKindOfClass:[NSDictionary class]]) {
return [NSString stringWithFormat:@"%ld kids",[[item objectForKey:@"children"] count]];
}
return item;
}
else{
if ([item isKindOfClass:[NSDictionary class]]) {
return [item objectForKey:@"parent"];
}
}
return nil;
}
再写一个方法。
并在IB中将NSOutlineView的委托设置为AppDelegate。