Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSOutlineView中数组枚举对象的自定义节_Objective C_Nsmutablearray_Nsoutlineview - Fatal编程技术网

Objective c NSOutlineView中数组枚举对象的自定义节

Objective c NSOutlineView中数组枚举对象的自定义节,objective-c,nsmutablearray,nsoutlineview,Objective C,Nsmutablearray,Nsoutlineview,我正在尝试为列出的对象创建带有自定义标题组(父节点)的NSOutlineView。(注意:我有基于单元格的NSOutlineView)。例如,它看起来像Xcode“导航器”或数字侧边栏。我使用了每个类别的分离属性的默认组,但它看起来不像我想要的那样。我需要一个父节点(单元),我可以直观地调整(添加一个控件元素和图像) 我试图通过向NSDictionary传递一个对象数组来实现这一点,给每个组一个特定的键。结果,通过NSLog,所有内容都正确显示,但将此变量作为程序NSOulineView的源传输

我正在尝试为列出的对象创建带有自定义标题组(父节点)的NSOutlineView。(注意:我有基于单元格的NSOutlineView)。例如,它看起来像Xcode“导航器”或数字侧边栏。我使用了每个类别的分离属性的默认组,但它看起来不像我想要的那样。我需要一个父节点(单元),我可以直观地调整(添加一个控件元素和图像)

我试图通过向NSDictionary传递一个对象数组来实现这一点,给每个组一个特定的键。结果,通过NSLog,所有内容都正确显示,但将此变量作为程序NSOulineView的源传输失败

ProjectViewController.h

@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject> {
    IBOutlet NSOutlineView          *outlineView;
    FSEntity                        *content;
}

@property (readonly, assign) NSMutableArray *objects;

@end
为了更容易想象它会是什么样子,我在方案中展示了它:

                 +--------------------------------------------+
                 |  ▼ FINDER FILES                        ₪ ✱ |
                 |      03143553.file                         |
                 |    ▶ Desktop                               |
                 |    ▶ Documents                             |
                 |    ▶ Downloads                             |
                 |    ▶ Movies                                |
                 |    ▶ Music                                 |
                 |    ▶ Pictures                              |
                 +--------------------------------------------+
以及我现在拥有的(不使用NSTreeController的NSOulineView)

我知道示例Apple“SourceView”,但我不知道如何将对象数组(文件和文件夹)添加到已创建的组中,NSTreeContoller仅显示层次结构的第一个元素(不包括):

SourceView的修改方法示例:

- (void)addFinderSection {
    [self addFolder:@"FINDER FILES"];

    NSError *error = nil;
    NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
    for (NSURL *url in urls) {
        BOOL isDirectory;
        if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
            if (isDirectory) {
                [self addChild:[url path] withName:NO selectParent:YES];
            } else {
                [self addChild:[url path] withName:NO selectParent:YES];   
            }
        }
    }

    [self selectParentFromSelection];
}
此方法仅显示第一个对象,如后一个方案中所示

还有一个问题,如我之前所说,如何添加到单元格右侧的节点**“FINDER FILES”**按钮


你能帮我吗?我知道,也许没那么难,但我刚刚开始学习Objective-C,我不知道怎么做。谢谢。

根据您提供的起始代码,我能够使用基于Cell的NSOutlineViews使某些功能正常工作。您发布的代码似乎不完整,因此很难知道您希望从丢失的FSEntity类中得到什么。我刚刚使用基础类:NSCORE用于节点列表(即根节点和子节点),每个节点本身的NS字典(根节点和子节点),以及作为文件系统引用的NoSQL。我试着尽可能接近你的原始代码。最后,它看起来像这样:

ProjectViewController.h

@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject>
{
    IBOutlet NSOutlineView          *outlineView;
    NSURL                           *content;
}

@end
@interface ProjectViewController:NSViewController
{
IBOutlet NSOutlineView*outlineView;
NSURL*内容;
}
@结束
ProjectViewController.m

#import "ProjectViewController.h"

@interface ProjectViewController () {
    NSMutableArray* _objects;
}
@property (nonatomic, retain, readwrite) NSMutableArray* objects;
@end

@implementation ProjectViewController

@synthesize objects = _objects;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Initialization code here.
        // Setting default path to the local file or directory
        NSString *home = NSHomeDirectory();
        content = [[NSURL alloc] initFileURLWithPath:home];

        [self defineContentNSOutlineView];
        NSLog(@"Array: %@",_objects);

        // Basic сonfiguration an instance NSOutlineView
        [self configurationNSOutlineView];
    }
    return self;
}

// nodes have 3 keys: title, url, icon
- (NSArray*)p_childrenForNode: (NSMutableDictionary*)node {
    if (nil == node)
        return self.objects;

    NSArray* retVal = nil;
    if (nil == (retVal = [node valueForKey: @"children"]))
    {
        NSMutableArray* children = [NSMutableArray array];
        for (NSURL* urlInDir in [[NSFileManager defaultManager] contentsOfDirectoryAtURL: [node objectForKey: @"url"]
                                                           includingPropertiesForKeys: [NSArray arrayWithObjects: NSURLNameKey, NSURLEffectiveIconKey, nil]
                                                                              options: 0
                                                                                error: NULL])
        {
            id name = [urlInDir getResourceValue: &name forKey: NSURLNameKey error: NULL] ? name : @"<Couldn't get name>";
            id icon = [urlInDir getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;

            NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: urlInDir, @"url", name, @"title", nil];

            if (icon)
                [dict setObject: icon forKey: @"icon"];

            [children addObject: dict];
        }

        retVal = children;

        if (children)
            [node setValue: children forKey: @"children"];
    }
    return retVal;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > index ? [[[children objectAtIndex: index] retain] autorelease] : nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > 0;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    NSInteger retVal = children.count;
    return retVal;
}

- (id)outlineView:(NSOutlineView *)pOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {

    NSImage* icon = [item objectForKey: @"icon"];
    NSString* title = [item objectForKey: @"title"];
    id value = nil;

    if (icon) {
        [icon setSize: NSMakeSize(pOutlineView.rowHeight - 2, pOutlineView.rowHeight - 2)];
        NSTextAttachment* attachment = [[[NSTextAttachment alloc] init] autorelease];
        [(NSCell *)[attachment attachmentCell] setImage: icon];
        NSMutableAttributedString *aString = [[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy] autorelease];
        [[aString mutableString] appendFormat: @" %@", title];
        value = aString;
    } else {
        value = title;
    }

    return value;
}

- (void)defineContentNSOutlineView {
    // Make root object
    NSMutableDictionary* rootObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"FINDER", @"title",
                                    content, @"url",
                                    nil];

    id icon = [content getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;
    if (icon)
        [rootObj setObject: icon forKey: @"icon"];

    // Set it
    self.objects = [NSMutableArray arrayWithObject: rootObj];
}

- (void)configurationNSOutlineView {
    [outlineView sizeLastColumnToFit];
    [outlineView setFloatsGroupRows:NO];
    [outlineView reloadData];
    [outlineView expandItem:nil expandChildren:YES];
}

@end
#导入“ProjectViewController.h”
@接口ProjectViewController(){
NSMutableArray*_对象;
}
@属性(非原子、保留、读写)NSMutableArray*对象;
@结束
@ProjectViewController的实现
@合成对象=_对象;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
if(self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
//这里是初始化代码。
//设置本地文件或目录的默认路径
NSString*home=nshomeditory();
content=[[NSURL alloc]initFileURLWithPath:home];
[自定义内容大纲视图];
NSLog(@“数组:%@”,_对象);
//基本配置实例NSOutlineView
[自配置NSOutlineView];
}
回归自我;
}
//节点有3个键:标题、url、图标
-(NSArray*)p_childrenForNode:(NSMutableDictionary*)节点{
if(nil==节点)
返回自我对象;
NSArray*retVal=nil;
if(nil==(retVal=[node valueForKey:@“children”])
{
NSMutableArray*子项=[NSMutableArray];
对于[[NSFileManager defaultManager]目录属性:[node objectForKey:@“url]中的(NSURL*urlInDir)
includingPropertiesForKeys:[n包含对象的数组:NSURLNameKey、nsurEffectiveIConkey、nil]
选项:0
错误:NULL])
{

id name=[urlInDir getResourceValue:&name-forKey:NSURLNameKey错误:NULL]?name:@“我意识到这可能并不完全有用,但我怀疑您会发现使用基于NSView的NSOutlineView(与基于NSCell的NSView相比)实现这一点要容易得多。”。原因是,使用基于视图的NSOutlineViews,您可以添加任意数量的子视图,并保留其所有标准功能。使用基于单元格的方法,您将只能使用单个单元格,并将多个控件的行为组合在一起,这将涉及编写自定义NSCell子类和大量自定义绘图和事件句柄基于视图的NSOutlineView将免费提供给您。
- (void)addFinderSection {
    [self addFolder:@"FINDER FILES"];

    NSError *error = nil;
    NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
    for (NSURL *url in urls) {
        BOOL isDirectory;
        if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
            if (isDirectory) {
                [self addChild:[url path] withName:NO selectParent:YES];
            } else {
                [self addChild:[url path] withName:NO selectParent:YES];   
            }
        }
    }

    [self selectParentFromSelection];
}
@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject>
{
    IBOutlet NSOutlineView          *outlineView;
    NSURL                           *content;
}

@end
#import "ProjectViewController.h"

@interface ProjectViewController () {
    NSMutableArray* _objects;
}
@property (nonatomic, retain, readwrite) NSMutableArray* objects;
@end

@implementation ProjectViewController

@synthesize objects = _objects;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Initialization code here.
        // Setting default path to the local file or directory
        NSString *home = NSHomeDirectory();
        content = [[NSURL alloc] initFileURLWithPath:home];

        [self defineContentNSOutlineView];
        NSLog(@"Array: %@",_objects);

        // Basic сonfiguration an instance NSOutlineView
        [self configurationNSOutlineView];
    }
    return self;
}

// nodes have 3 keys: title, url, icon
- (NSArray*)p_childrenForNode: (NSMutableDictionary*)node {
    if (nil == node)
        return self.objects;

    NSArray* retVal = nil;
    if (nil == (retVal = [node valueForKey: @"children"]))
    {
        NSMutableArray* children = [NSMutableArray array];
        for (NSURL* urlInDir in [[NSFileManager defaultManager] contentsOfDirectoryAtURL: [node objectForKey: @"url"]
                                                           includingPropertiesForKeys: [NSArray arrayWithObjects: NSURLNameKey, NSURLEffectiveIconKey, nil]
                                                                              options: 0
                                                                                error: NULL])
        {
            id name = [urlInDir getResourceValue: &name forKey: NSURLNameKey error: NULL] ? name : @"<Couldn't get name>";
            id icon = [urlInDir getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;

            NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: urlInDir, @"url", name, @"title", nil];

            if (icon)
                [dict setObject: icon forKey: @"icon"];

            [children addObject: dict];
        }

        retVal = children;

        if (children)
            [node setValue: children forKey: @"children"];
    }
    return retVal;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > index ? [[[children objectAtIndex: index] retain] autorelease] : nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > 0;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    NSInteger retVal = children.count;
    return retVal;
}

- (id)outlineView:(NSOutlineView *)pOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {

    NSImage* icon = [item objectForKey: @"icon"];
    NSString* title = [item objectForKey: @"title"];
    id value = nil;

    if (icon) {
        [icon setSize: NSMakeSize(pOutlineView.rowHeight - 2, pOutlineView.rowHeight - 2)];
        NSTextAttachment* attachment = [[[NSTextAttachment alloc] init] autorelease];
        [(NSCell *)[attachment attachmentCell] setImage: icon];
        NSMutableAttributedString *aString = [[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy] autorelease];
        [[aString mutableString] appendFormat: @" %@", title];
        value = aString;
    } else {
        value = title;
    }

    return value;
}

- (void)defineContentNSOutlineView {
    // Make root object
    NSMutableDictionary* rootObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"FINDER", @"title",
                                    content, @"url",
                                    nil];

    id icon = [content getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;
    if (icon)
        [rootObj setObject: icon forKey: @"icon"];

    // Set it
    self.objects = [NSMutableArray arrayWithObject: rootObj];
}

- (void)configurationNSOutlineView {
    [outlineView sizeLastColumnToFit];
    [outlineView setFloatsGroupRows:NO];
    [outlineView reloadData];
    [outlineView expandItem:nil expandChildren:YES];
}

@end