zoukankan      html  css  js  c++  java
  • #在蓝懿学习iOS的日子#第五个练习日

    今天主要复习了昨天学习的文件管理器的知识,由于之前练的比较少,思维逻辑有些跟不上

    1、我们要搭建好界面,创建好界面

    //创建一个以文件名开头的可变数组

    @property (nonatomic,strong)NSMutableArray *filePaths;

    @end

     

    @implementation TableViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        //初始化数组

        self.filePaths = [NSMutableArray array];

        //判断跳转页面如果是第一次,就是显示固定路径下面的内容

        if (!self.directoryPath) {

            self.directoryPath = @"/Volumes/文件存放/蓝懿二期";

        }

       //跳转页面后通过复用显示文件名

        self.title=[self.directoryPath lastPathComponent];

        //显示的文件夹的路径

        NSString*path = self.directoryPath;

        //获取文件名,显示

        NSFileManager* fm = [NSFileManager defaultManager];

        NSArray *fileNames= [fm contentsOfDirectoryAtPath:path error:nil];

        //把隐藏文件的过滤

        for (NSString*fileName in fileNames) {

            //判断文件名以什么开头(hasPrefix)的文件

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

                //字符串的拼接

                NSString *filePath = [path stringByAppendingPathComponent:fileName];

                //filePath添加到数组里边

                [self.filePaths addObject:filePath];

                

            }

        }

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

     

    //有多少行

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.filePaths.count;

    }

     

    //cell里显示的内容

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

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

    //取到每一行

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

       //完整路径的展示 lastPathComponent

        cell.textLabel.text = [filePath lastPathComponent];

        //判断是否是文件夹显示右箭头

        BOOL isDir = NO;

        NSFileManager *fm = [NSFileManager defaultManager];

        if ([fm fileExistsAtPath:filePath isDirectory:&isDir]&&isDir) {//是文件夹

            

            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]

            ;

            //添加图片显示文件夹

            cell.imageView.image = [UIImage imageNamed:@"direcotry.png"];

        }else{//如果不是文件夹 什么都不显示

             [cell setAccessoryType:UITableViewCellAccessoryNone];

              //判断是否是什么类型的文件显示的图片

            //判断文件名以什么结尾(hasSuffix)的文件

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

                

                

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

                

                

            }else if([filePath hasSuffix:@".jpg"]||[filePath hasSuffix:@".png"]||[filePath hasSuffix:@".JPG"]||[filePath hasSuffix:@".PNG"]){

                cell.imageView.image =[UIImage imageWithContentsOfFile:filePath];

            }else{

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

            }

        }

        

        

        return cell;

    }

    //当点击某一行cell进去

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

        //点击的路径是什么

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

          //判断是否是文件夹//跳转页面

        BOOL isDir = NO;

        NSFileManager *fm = [NSFileManager defaultManager];

        if ([fm fileExistsAtPath:filePath isDirectory:&isDir]&&isDir) {//是文件夹

            

            //复用当前页面 storyboard通过创建 TableViewController来区分

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

            //一个页面正向传值

            vc.directoryPath = filePath;

            

            [self.navigationController pushViewController:vc animated:YES];

        }else{//如果不是文件夹 什么都不显示

            

            //判断文件名以什么结尾(hasSuffix)的文件

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

                

                //创建空页面显示内容,进行展示

                UIViewController*vc = [[UIViewController alloc]init];

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

                [vc.view addSubview:tv];

                tv.text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

                [self.navigationController pushViewController:vc animated:YES];

                

                

            }else if([filePath hasSuffix:@".jpg"]||[filePath hasSuffix:@".png"]||[filePath hasSuffix:@".JPG"]||[filePath hasSuffix:@".PNG"]){

                //创建空页面显示内容,进行展示

                UIViewController*vc = [[UIViewController alloc]init];

                //添加背景颜色

                vc.view.backgroundColor =[UIColor blackColor];

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

                [vc.view addSubview:iv];

                iv.image = [UIImage imageWithContentsOfFile:filePath];

                [iv setContentMode:UIViewContentModeScaleAspectFill];

                [self.navigationController pushViewController:vc animated:YES];

            }else{//其他文件

               

                //添加  展示

                

                UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"该版本暂不支持此格式,请期待下一版本!" preferredStyle:UIAlertControllerStyleAlert];

                [a addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];

                

                [self presentViewController:a animated:YES completion:nil];

            }

            

        }

        

       

        

    }

  • 相关阅读:
    VS2010、SQL Server 2008和SQL Server 2012安装详解
    IIS服务寄宿
    C#中错误:不包含适合于入 口点的静态“Main”方法 的解决方法
    硬件的一些性能指标
    SATA SAS SSD 硬盘介绍和评测
    mysql数据库锁定机制
    mysql日志设置优化
    MySQL硬件瓶颈分析
    可扩展性设计之数据切分
    硬件环境对系统性能的影响
  • 原文地址:https://www.cnblogs.com/odileye/p/4954067.html
Copyright © 2011-2022 走看看