获取本地文件并显示在tableView中,如同QQ中的文件展示,但是还不能查看内容
2017-01-09 18:30
288 查看
NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
// NSLog(@"%@",docDir);
NSError *error = nil;
_dataFileArray = [[NSArray alloc] init];
//_dataFileArray便是包含有该文件夹下所有文件的文件名及文件夹名的数组
_dataFileArray = [fileManager contentsOfDirectoryAtPath:docDir error:&error];
//列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
//在上面那段程序中获得的_dataFileArray中列出文件夹名
for (NSString *file in _dataFileArray) {
NSString *path = [docDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataFileArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TaskFileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"taskFileCell" forIndexPath:indexPath];
cell.titleLabel.text = [_dataFileArray objectAtIndex:indexPath.row];
NSArray *strDivision = [cell.titleLabel.text componentsSeparatedByString:@"."];
NSString *strIntercept = [strDivision lastObject];
if ([strIntercept isEqualToString:@"plist"]) {
cell.fileImage.image = [UIImage imageNamed:@"plista.png"];
}
if ([strIntercept isEqualToString:@"doc"]) {
cell.fileImage.image = [UIImage imageNamed:@"doc.png"];
}
if ([strIntercept isEqualToString:@"html"]) {
cell.fileImage.image = [UIImage imageNamed:@"htmlaa.png"];
}
if ([strIntercept isEqualToString:@"ppt"]) {
cell.fileImage.image = [UIImage imageNamed:@"ppt.png"];
}
if ([strIntercept isEqualToString:@"rar"]) {
cell.fileImage.image = [UIImage imageNamed:@"rara.png"];
}
if ([strIntercept isEqua
4000
lToString:@"zip"]) {
cell.fileImage.image = [UIImage imageNamed:@"zipa"];
}
if ([strIntercept isEqualToString:@"xls"]) {
cell.fileImage.image = [UIImage imageNamed:@"xlsa"];
}
if ([strIntercept isEqualToString:@"txt"]) {
cell.fileImage.image = [UIImage imageNamed:@"txta.png"];
}
if ([strIntercept isEqualToString:@"png"] || [strIntercept isEqualToString:@"jpg"]) {
//获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *strRoute = [NSString stringWithFormat:@"%@/%@",docDir,cell.titleLabel.text];
cell.fileImage.image = [UIImage imageNamed:strRoute];
}
return cell;
}
注:本文版权归作者所有,转载请注明出处
http://www.jianshu.com/p/a26cf0e316fe
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
// NSLog(@"%@",docDir);
NSError *error = nil;
_dataFileArray = [[NSArray alloc] init];
//_dataFileArray便是包含有该文件夹下所有文件的文件名及文件夹名的数组
_dataFileArray = [fileManager contentsOfDirectoryAtPath:docDir error:&error];
//列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
//在上面那段程序中获得的_dataFileArray中列出文件夹名
for (NSString *file in _dataFileArray) {
NSString *path = [docDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataFileArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TaskFileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"taskFileCell" forIndexPath:indexPath];
cell.titleLabel.text = [_dataFileArray objectAtIndex:indexPath.row];
NSArray *strDivision = [cell.titleLabel.text componentsSeparatedByString:@"."];
NSString *strIntercept = [strDivision lastObject];
if ([strIntercept isEqualToString:@"plist"]) {
cell.fileImage.image = [UIImage imageNamed:@"plista.png"];
}
if ([strIntercept isEqualToString:@"doc"]) {
cell.fileImage.image = [UIImage imageNamed:@"doc.png"];
}
if ([strIntercept isEqualToString:@"html"]) {
cell.fileImage.image = [UIImage imageNamed:@"htmlaa.png"];
}
if ([strIntercept isEqualToString:@"ppt"]) {
cell.fileImage.image = [UIImage imageNamed:@"ppt.png"];
}
if ([strIntercept isEqualToString:@"rar"]) {
cell.fileImage.image = [UIImage imageNamed:@"rara.png"];
}
if ([strIntercept isEqua
4000
lToString:@"zip"]) {
cell.fileImage.image = [UIImage imageNamed:@"zipa"];
}
if ([strIntercept isEqualToString:@"xls"]) {
cell.fileImage.image = [UIImage imageNamed:@"xlsa"];
}
if ([strIntercept isEqualToString:@"txt"]) {
cell.fileImage.image = [UIImage imageNamed:@"txta.png"];
}
if ([strIntercept isEqualToString:@"png"] || [strIntercept isEqualToString:@"jpg"]) {
//获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *strRoute = [NSString stringWithFormat:@"%@/%@",docDir,cell.titleLabel.text];
cell.fileImage.image = [UIImage imageNamed:strRoute];
}
return cell;
}
注:本文版权归作者所有,转载请注明出处
http://www.jianshu.com/p/a26cf0e316fe
相关文章推荐
- 从指定网址下载文件 并读出其内容 && 从网上下载文件放在本地&&从网上获取图片设在imageView里
- Swift 获取plist文件展示在TableView上
- 远程获取内容,并将内容存在本地电脑上,包括任何文件
- 打开网页提示:为了保护你的安全,internet explorer已经限制此文件显示只要能访问您 的计算机的活动内容,单击此处查看选项。如何处理
- swing实现拖拽效果(将本地文件拖拽到程序的文本框中显示内容)
- iOS学习笔记---数据库SQLite3的基本操作并讲内容显示在tableView中
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- 我的table有一备注型字段,我现在能在Dbrid中显示这字段,但是不能在Dbgrid上编辑,如何才能编辑该字段?
- 查看rpm包内容和解压获取rpm包文件
- 某些CHM文件不能查看内容的解决方法
- android 读取sd卡文件的内容用TextView显示
- IE10不能显示JSON文件内容
- 移动硬盘新加卷变为本地磁盘且不能打开磁盘查看文件
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- 在c#中将dataset输出成xml内容的xls文件,解决其它输出不能在excel中显示“001”的前导0的问题
- 我的table有一备注型字段,我现在能在Dbrid中显示这字段,但是不能在Dbgrid上编辑,如何才能编辑该字段?
- XP专业版中安装了IIS,配置好之后只能访问静态htm网页,图片之类的,但是ASP 文件确不能读取,提示无法显示网页提示
- c# winform项目treeview控件绑定本地电脑磁盘信息,读取文件夹信息,显示文件夹内文件内容
- js依次读取本地文件并显示内容
- JavaScript 仿LightBox内容显示效果 创建个LightBox.htm文件粘进去,直接查看效果