Using NSOutlineView as a file browser, starting from a given directory

徘徊边缘 提交于 2020-01-02 21:05:12

问题


I've been following this tutorial for using NSOutlineView as a hierarchical file browser:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/OutlineView/Articles/UsingOutlineDataSource.html

I used all the code in the tutorial and it worked. However, I then tried to call initWithPath: with a path other than / and it doesn't work: the fullPath of the top item (i.e. the folder specified in initWithPath) is just the name of the folder, and the children method of the FileSystemItem returns an empty array, I assume because the file manager is looking in /FolderName/ rather than the absolute path, which never appears to be saved.

How could I modify this code to allow it to do this?


回答1:


The code above almost works. As soon as you try to open a folder it crashes. Try it with this modification. It works perfectly for me:

- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
  if (self = [super init]) {
    relativePath = [path copy];
    parent = parentItem;
  }
 return self;
}



回答2:


Modify the following method:

- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
    self = [super init];
    if (self) {
        relativePath = path; //[[path lastPathComponent] copy];
        NSLog(@"%@",relativePath);
        parent = parentItem;
    }
    return self;
}

The original logic assumes that you would be starting at root. This should make it work at any path.




回答3:


Try this code - https://github.com/johndpope/TreeTest

//
//  ViewController.m
//  TreeTest
//
//  Created by Zakk Hoyt on 7/9/14.
//  Copyright (c) 2014 Zakk Hoyt. All rights reserved.
//

#import "ViewController.h"
#import "VWWContentItem.h"
#import "FileSystemItem.h"


typedef void (^VWWEmptyBlock)(void);
typedef void (^VWWCLLocationCoordinate2DBlock)(CLLocationCoordinate2D coordinate);
typedef void (^VWWBoolDictionaryBlock)(BOOL success, NSDictionary *dictionary);





@interface ViewController ()
@property (weak) IBOutlet NSOutlineView *outlineView;
@property (weak) IBOutlet NSPathControl *pathControl;
//@property (strong) NSMutableArray *contents;
@property (strong) VWWContentItem *item;
@property (strong) NSIndexSet *selectedIndexes;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    NSString *picturesPath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), @"Pictures"];
    self.pathControl.URL = [NSURL fileURLWithPath:picturesPath];
//    [FileSystemItem rootItemWithPath:self.pathControl.URL.path];
//    [self seachForFilesInDirectory:picturesPath];


}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.

}
- (IBAction)pathControlAction:(NSPathControl *)sender {

}




// Data Source methods

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(FileSystemItem*)item {
    return (item == nil) ? 1 : [item numberOfChildren];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(FileSystemItem*)item {
    return (item == nil) ? YES : ([item numberOfChildren] != -1);
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(FileSystemItem*)item {
//    return (item == nil) ? [FileSystemItem rootItem] : [(FileSystemItem *)item childAtIndex:index];
    return (item == nil) ? [FileSystemItem rootItemWithPath:self.pathControl.URL.path] : [(FileSystemItem *)item childAtIndex:index];
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(FileSystemItem*)item {
    if([tableColumn.identifier isEqualToString:@"tree"]){
        return (item == nil) ? @"/" : (id)[item relativePath];
//        return (item == nil) ? @"Pictures" : (id)[item relativePath];
    } else if([tableColumn.identifier isEqualToString:@"coordinate"]){
        return @"coordinate";
    }

    return nil;
//    return (item == nil) ? self.pathControl.URL.path : (id)[item relativePath];
}

// Delegate methods

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(FileSystemItem*)item {
    return NO;
}

@end


来源:https://stackoverflow.com/questions/10636416/using-nsoutlineview-as-a-file-browser-starting-from-a-given-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!