我一直在关注本教程,将NSOutlineView用作分层文件浏览器:
我使用了教程中的所有代码并且它有效。但是,我尝试使用initWithPath:
以外的路径调用/
并且它不起作用:顶部项目的fullPath
(即initWithPath
中指定的文件夹)只是文件夹的名称,children
的{{1}}方法返回一个空数组,我假设因为文件管理器正在查找FileSystemItem
而不是绝对路径,永远不会被保存。
我如何修改此代码以允许它执行此操作?
答案 0 :(得分:4)
上面的代码几乎可以使用。只要您尝试打开文件夹,它就会崩溃。尝试使用此修改。它对我来说很完美:
- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
if (self = [super init]) {
relativePath = [path copy];
parent = parentItem;
}
return self;
}
答案 1 :(得分:2)
修改以下方法:
- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
self = [super init];
if (self) {
relativePath = path; //[[path lastPathComponent] copy];
NSLog(@"%@",relativePath);
parent = parentItem;
}
return self;
}
原始逻辑假定您将从root开始。这应该使它适用于任何道路。
答案 2 :(得分: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