1

我一直在按照本教程使用 NSOutlineView 作为分层文件浏览器:

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

我使用了教程中的所有代码并且它工作正常。但是,然后我尝试initWithPath:使用除此之外的路径进行调用,/但它不起作用:fullPath顶部项目(即在 中指定的文件夹initWithPath)只是文件夹的名称,并且该children方法FileSystemItem返回一个空数组,我假设是因为文件管理器正在查找/FolderName/而不是绝对路径,它似乎永远不会被保存。

我如何修改此代码以允许它执行此操作?

4

3 回答 3

4

上面的代码几乎可以工作。一旦您尝试打开文件夹,它就会崩溃。试试这个修改。它非常适合我:

- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
  if (self = [super init]) {
    relativePath = [path copy];
    parent = parentItem;
  }
 return self;
}
于 2012-08-04T21:54:53.827 回答
2

修改以下方法:

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

原始逻辑假定您将从根目录开始。这应该使它可以在任何路径上工作。

于 2012-07-22T00:07:24.240 回答
0

试试这个代码 - 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
于 2015-02-12T07:47:27.483 回答