【问题标题】:Using NSOutlineView as a file browser, starting from a given directory使用 NSOutlineView 作为文件浏览器,从给定目录开始
【发布时间】:2012-05-17 13:12:59
【问题描述】:

我一直在学习使用 NSOutlineView 作为分层文件浏览器的教程:

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

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

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

【问题讨论】:

  • 我遇到了同样的问题,你能帮我解决我的错误吗?我发布了here
  • 我在等待回复。我遵循相同的代码。 bt 我无法刷新它。如果在查看时将新文件夹添加到编程或查找器,则不会刷新。如何做到这一点。
  • 你能帮帮我吗?我还在等你的回复

标签: objective-c cocoa nsfilemanager nsoutlineview


【解决方案1】:

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

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

【讨论】:

  • 这是我的Post。我在等待回复。我遵循相同的代码。 bt 我无法刷新它。如果在查看时将新文件夹添加到编程或查找器,则不会刷新。如何做到这一点。
【解决方案2】:

修改如下方法:

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

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

【讨论】:

  • 这是我的Post。我在等待回复。我遵循相同的代码。 bt 我无法刷新它。如果在查看时将新文件夹添加到编程或查找器,则不会刷新。如何做到这一点。
【解决方案3】:

试试这个代码 - 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2015-03-28
    相关资源
    最近更新 更多