#在蓝懿学习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];

}

}

}

时间: 2024-08-05 07:03:41

#在蓝懿学习iOS的日子#第五个练习日的相关文章

#在蓝懿学习iOS的日子#第四个练习日

转眼间,时间过得很快,来这里学习快要一个月了,经历了7天课程的零基础训练营,认识了iOS,学习了一些简单的的知识,紧接着进入 了正式的基础课,已经上了的课,学习了好多的东西: 学习的内容有:变量. 常用数字运算符. 控件.变量的作用域.if逻辑判断.swith-case.for循环.Timer . 讲了while语句.break:跳出当前的循环:continue:结束本次循环,进入下次循环:return:结束当前的方法.面向对象.类方法.工厂方法.单例.继承.多态.数组.遍历.协议内存.管理内存

#在蓝懿学习iOS的日子#第七个练习日

复习这两天学习的内容 动画 //    frame  bounds  alpha  背景颜色  center  transform(转换)    UIImageView* iv =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    iv.image = [UIImage imageNamed:@"8.jpg"];    [self.view addSubview:iv];    //NO.1旋转渐变

#在蓝懿学习iOS的日子#Day10

#在蓝懿学习iOS的日子#Day10今天做了一个大的游戏,涵盖了这段时间学洗的知识,页面的切换字符串,还有可变数组和遍历等知识点. 1.显示搭建视图添加背景,创建一个选择hero视图,添加一个button点击进入下一个选择hero视图: 2.在hero视图搭建视图添加背景,创建一个英雄hero类,把不同的英雄j连接进同一个button,设置tag用以区分hero类:在点击button进入游戏页面, 3.在游戏页面添加移动的背景视图, -(void)initBG{ //添加背景图片 self.bg

#在蓝懿学习iOS的日子#Day2

#在蓝懿学习iOS的日子#今天的学习的东西好多,当是每天的笔记吧: 1. 变量的作用域 局部变量:只可以在大括号内{}的范围为内使用: 全局变量: 在@interface ViewController ()的大括号内{}设置的变量u都j是全局变量,但不可以赋值 例:@interface ViewController (){ UIImageView *bulletIV; float x; int m; } 2.布尔值:BOOL  取值0或1   即:1是表示条件成立,0表示条件不成立: 3.if语

#在蓝懿学习iOS的日子#第二个练习日

#在蓝懿学习iOS的日子#第二个练习日,在学习Day4和Day5的知识后, 今天主要的任务是:巩固和加深这两天学习的内容,这俩天,主要学习的是类的创建,如何正在.h头文件和.m子文件里输入相关的属性及方法,把一个对象的属性和方法,封装到一个类.m子文件里面,然后在在ViewContrller.m里可以直接调用类的属性及方法,感觉条理分明,更多的东西不会杂乱无章,变得有章可循. 又分别 讲了inWith - (instancetype)initWithName:(NSString *)name a

#在蓝懿学习iOS的日子#day14

今天上午学习uitabelViewcell和下午学习用uitabelViewcel来显示手机播放器的客户端显示歌词的界面,和字典 //    创建不可变字典 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"18",@"age", nil]; dic = @{@"name":@"

#在蓝懿学习iOS的日子#Day7

今天学习内存管理的内容,比较抽象,应用的不是很多,但还是要理解,那现在我就来回顾一下,学习的内容吧! 一.id:任意对象 self:调用自身类 super:引用父类本身的属性 二.内存管理 ARC:自动内存管理 MRC:手动内存管理 1.内存计数机制:内存计数表示当前对象被引用的次数,如果引用次数为0 则对象会从内存中释放掉 retain会对内存计数+1 release会对内存计数-1 2.retain(strong)assign(weak)copy对set方法的影响: retain:两件事1.

#在蓝懿学习iOS的日子#Day5

由于昨天学习内容比较多,相似度比较大,难以区分,今天刘国斌老师给我们 做了巩固,为了更好地理解面向对象, 我们先是讲了在没有运用面向对象讲了如何让一个有一组僵尸的图片移动,感觉就像把人分解的动作画在一个本子里,在翻动本子的同时,人就慢慢地动起来了,感觉用面向对象的方式,把一个对象的属性和方法,封装到一个类里面,然后在在ViewContrller.m里可以直接调用类的属性及方法,感觉条理分明,更多的东西不会杂乱无章,变得有章可循. 接着我们讲了我们要创建一个类,点击command+N可以创建一个类

#在蓝懿学习iOS的日子#Day12

1.搭建界面关联控件 把多个文本输入框关联到一个数组中 2.创建一个生成随机数的方法 返回值为nsstring 有一个参数 类型为long类型 3.在ViewDidload里面 让每一个文本输入框里面显示一个一位的随机数(因为程序运行起来默认选中的是1),开启一个timer去修改时间的label 4.当点击单选控件的时候 根据选择的数字去让数组中所有文本输入框 显示相应位数的随机数,让时间清零 5.当点击我记住了按钮时让时间停止,让菊花停止 把数字用一个数组记录 清空每一个文本输入框 6.点击开