美文网首页
NSFileManager遍历文件夹

NSFileManager遍历文件夹

作者: 喵喵粉 | 来源:发表于2024-01-05 16:39 被阅读0次
 DirListVC *vc = [[DirListVC alloc] init];
 vc.rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
 [self.navigationController pushViewController:vc animated:YES];

DirListVC.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface DirListVC : UIViewController
@property (nonatomic, strong) NSString *rootPath;
@end

NS_ASSUME_NONNULL_END

DirListVC.m

#import "DirListVC.h"

@interface DirListVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *items;
@end

@implementation DirListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self initData];
}

- (void)initData {
    if (!_rootPath) {
        //_rootPath = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES).lastObject;
        _rootPath = NSHomeDirectory();
    }
    self.navigationItem.title = _rootPath;
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray<NSString *> *list = [fm contentsOfDirectoryAtPath:_rootPath error:nil];
    NSLog(@"%@", list);
    _items = list;
    [self.tableView reloadData];
}

- (void)initView {
    [self.view addConstraints:@[
        [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0],
        [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0],
        [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0],
        [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0],
    ]];
}
//要传完整路径
- (BOOL)isDirectory:(NSString *)filePath {
  BOOL isDirectory = NO;
  [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  return isDirectory;
}
//要传完整路径
- (BOOL)isDirectory1:(NSString *)filePath {
  NSNumber *isDirectory;
  NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
  [fileUrl getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
  return isDirectory.boolValue;
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    
    NSString *path = _items[indexPath.row];
    BOOL dir = [self isDirectory1:[_rootPath stringByAppendingPathComponent:path]];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", dir ? @"📁": @"📃", path];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *path = _items[indexPath.row];
    BOOL dir = [self isDirectory1:[_rootPath stringByAppendingPathComponent:path]];
    if (!dir) {
        return;
    }
    DirListVC *vc = [[DirListVC alloc] init];
    vc.rootPath = [_rootPath stringByAppendingPathComponent:path];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end

相关文章

网友评论

      本文标题:NSFileManager遍历文件夹

      本文链接:https://www.haomeiwen.com/subject/sssindtx.html