IOS 递归实现文件管理器的小例子

1.文件管理器的解决方案:

2.创建一个UITABLEVIEWCONTROLLER的tableviewcontroller的文件:对页面进行关联, ceLL标识关 联;

3.需要在布局的时候关联一下 显示一个删除的按钮   用Navigation。

4.设置一个访问文件的路径。根目录   这样你可以继续打开它下层的东西     ;获取这个文件夹下所有路径的内容:就是刚才定义的

[selffindFileInDirecotryPath:@"/Users/bmuyu/Desktop"];

5.获取文件夹下层内容: 即用数组遍历的形式获取文件的名称:

for (NSString *fileName in fileNames) {

 NSString *filePath = [path stringByAppendingPathComponent:fileName];得到完整路径!

6.得到完整路径后,要显示到TableView中需要添加数组,  用数组之前切记要初始化(数据源数组)

7.将数据加到数据源数组中,  将pathName放到数据源数组:pathnames里,数据源数组就有数据了。就能用了

8.数据元数组有数据了,但是得在cell中显示,所以需要returnself.filePaths.count;

9.下一步cell每行显示的内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];

    

    NSString *filePath = self.filePaths[indexPath.row];

    

    cell.textLabel.text =[filePath lastPathComponent]; //此句话的意思就是不想看见全部路径,只看末尾的路径显示出来如图11

10.这些做完以后,画面中会显示路径,但也会显示出隐藏文件: 就是带"."的文件。如果不想看见的话  就在遍历数组的时候进行判断

for (NSString *fileName in fileNames) {

        if ([fileName hasPrefix:@"."]) {

            continue;

        }

        NSString *filePath =[path stringByAppendingPathComponent:fileName];

        [self.filePathsaddObject:filePath];

如果前面带点的话 就跳出本次循环 继续下次循环。

11.如果我不想看到完整的路径:如图  需要做什么改变呢

12.cell.textLabel.text =[filePath lastPathComponent];//此句话的意思就是不想看见全部路径,只看末尾的路径显示出来如图11

  1.  就显示出来了:那么 下一个需求就是:当 是文件夹的情况,右侧显示一个右箭头,作为跳转的(文件/文件夹的区分):
  2. 创建一个方法:实现判断是否是文件夹:

-(BOOL)isDirecotryWithPath:(NSString *)path{

    

    NSFileManager *fm = [NSFileManagerdefaultManager];

    BOOL isDir = NO;

    if ([fm fileExistsAtPath:path isDirectory:&isDir]&&isDir) {

        returnYES;

    }

    

    returnNO;

}

 //如果是文件夹再右侧添加右箭头

    if ([selfisDirecotryWithPath:filePath]) {

          [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        

        cell.imageView.image = [UIImageimageNamed:@"directory.png"];

        

    }else{//如果不是文件夹要把右箭头去掉  避免复用时出问题

        [cell setAccessoryType:UITableViewCellAccessoryNone];

如果不是文件夹的话  如果出去一个Cell(可能是有箭头的) 下面新刷新的可能出现服用的情况,会出现服用很多次的情况出现问题

  1. 现在出现的情况是 显示是可以显示出来了:但是没有图片,没有跳转, 所以现在需要加点击事件 didselect  deq那个东西该上场了!
  2. Cell点击事件的代码需要自己写:下面是代码    点击事件后需要进行跳转页面,因为不法知道页面的数量,也不可能在页面中创建出来

所以需要页面复用!!!

17.跳转页面后遇到的情况是:   首先需要在当前的页面中创建出新的页面,但是!!创建出来的页面需要你传递参数,所以你的参数值需要变

否则就会出现,你点击多少次,都是和上一个页面相同页面的效果!!  所以这时候需要在进行一次路径的修改,声明一个新的参数用来保存路径

然后判断页面是否存在第一次加载的情况:  如果是第一次加载就默认为根目录的路径;如果不是,就赋值给一个新的路径!

还有个情况就是,如果不是文件夹是不需要跳转的。是文件夹就需要跳转

以下是程序的相关代码:很重要!!!   

//判断是否是文件夹,是就跳转

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    

    NSString *filePath = self.filePaths[indexPath.row];

    

    if ([selfisDirecotryWithPath:filePath]) {//是文件夹

        //跳转页面

        TableViewController*vc = [self.storyboardinstantiateViewControllerWithIdentifier:@"TableViewController"];

        vc.directoryPath =filePath;

        [self.navigationControllerpushViewController:vc animated:YES];

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.filePaths = [NSMutableArrayarray];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //如果是第一次

    if (!self.directoryPath) {

        self.directoryPath = @"/Users/";

    }

    

    self.title =  [self.directoryPathlastPathComponent];  //当前文件夹的名称显示出来

    

    NSString *path = self.directoryPath;

  1. 以上代码的基本功能就基本实现了文本编辑器的大部分功能:剩余功能就是  如果是文件夹显示文件夹图片,其它。。 查看文本可以看

 //如果是文件夹再右侧添加右箭头

    if ([selfisDirecotryWithPath:filePath]) {

          [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        

        cell.imageView.image = [UIImageimageNamed:@"directory.png"];

        

    }else{//如果不是文件夹要把右箭头去掉  避免复用时出问题

        [cell setAccessoryType:UITableViewCellAccessoryNone];

        if ([filePath hasSuffix:@"txt"]||[filePathhasSuffix:@"rtf"]||[filePathhasSuffix:@"h"]||[filePathhasSuffix:@"m"]) {

            cell.imageView.image = [UIImageimageNamed:@"txt.png"];

        }elseif ([filePath hasSuffix:@"jpg"]||[filePathhasSuffix:@"png"]){

           //直接加载绝对路径的图片

            cell.imageView.image = [UIImageimageWithContentsOfFile:filePath];

        }else{//其它格式的文件

            cell.imageView.image = [UIImageimageNamed:@"other.png"];

            

        }

    }

    return cell;

(BOOL)isDirecotryWithPath:(NSString *)path{

    

    NSFileManager *fm = [NSFileManagerdefaultManager];

    

    BOOL isDir = NO;

    if ([fm fileExistsAtPath:path isDirectory:&isDir]&&isDir) {

        returnYES;

    }

    

    returnNO;

}

 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    

    NSString *filePath = self.filePaths[indexPath.row];

    

    if ([selfisDirecotryWithPath:filePath]) {//是文件夹

        //跳转页面

        TableViewController*vc = [self.storyboardinstantiateViewControllerWithIdentifier:@"TableViewController"];

        vc.directoryPath =filePath;

        [self.navigationControllerpushViewController:vc animated:YES];

        

    }else{//如果不是文件夹

        if ([filePath hasSuffix:@"txt"]||[filePathhasSuffix:@"rtf"]||[filePathhasSuffix:@"h"]||[filePathhasSuffix:@"m"]) {

           //创建一个页面里面显示一个textViewtv里显示文本

            UIViewController *vc =[[UIViewControlleralloc]init];

            UITextView *tv = [[UITextViewalloc]initWithFrame:vc.view.bounds];

            [vc.viewaddSubview:tv];

            vc.view.backgroundColor = [UIColorwhiteColor];

            tv.text = [NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:nil];

            [self.navigationControllerpushViewController:vc animated:YES];

            

            

            

        }elseif ([filePath hasSuffix:@"jpg"]||[filePathhasSuffix:@"png"]){

            UIViewController *vc =[[UIViewControlleralloc]init];

            UIImageView *iv = [[UIImageViewalloc]initWithFrame:vc.view.bounds];

            [vc.viewaddSubview:iv];

            iv.image = [UIImageimageWithContentsOfFile:filePath];

            

            [self.navigationControllerpushViewController:vc animated:YES];

            

        }else{//其它格式的文件

          

           UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"此版本暂不支持此格式,请期待下一版本!呵呵" delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定", nil];

            [alertView show];

            

        }

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSString *filePath = self.filePaths[indexPath.row];

        [[NSFileManagerdefaultManager] removeItemAtPath:filePatherror:nil];

        

        

        [self.filePathsremoveObjectAtIndex:indexPath.row];

        

        

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

    } elseif (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insertit into the array, and add a new row to the table view

    }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值