NSFileManager遍歷文件夾

 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市狼速,隨后出現(xiàn)的幾起案子愧驱,更是在濱河造成了極大的恐慌证逻,老刑警劉巖逸贾,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宠互,死亡現(xiàn)場離奇詭異浴讯,居然都是意外死亡擎厢,警方通過查閱死者的電腦和手機(jī)究流,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門辣吃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人芬探,你說我怎么就攤上這事神得。” “怎么了偷仿?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵哩簿,是天一觀的道長。 經(jīng)常有香客問我酝静,道長节榜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任别智,我火速辦了婚禮宗苍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘薄榛。我一直安慰自己讳窟,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布敞恋。 她就那樣靜靜地躺著挪钓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪耳舅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天倚评,我揣著相機(jī)與錄音浦徊,去河邊找鬼。 笑死天梧,一個(gè)胖子當(dāng)著我的面吹牛盔性,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播呢岗,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼冕香,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了后豫?” 一聲冷哼從身側(cè)響起悉尾,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挫酿,沒想到半個(gè)月后构眯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡早龟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年惫霸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了猫缭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡壹店,死狀恐怖猜丹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情硅卢,我是刑警寧澤射窒,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站老赤,受9級特大地震影響轮洋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抬旺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一弊予、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧开财,春花似錦汉柒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至历葛,卻和暖如春正塌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背恤溶。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工乓诽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咒程。 一個(gè)月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓鸠天,卻偏偏與公主長得像,于是被迫代替她去往敵國和親帐姻。 傳聞我的和親對象是個(gè)殘疾皇子稠集,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容