在UITableView的iOS文档目录中列出已保存的文件? [英] List saved files in iOS documents directory in a UITableView?
本文介绍了在UITableView的iOS文档目录中列出已保存的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已设置以下代码将文件保存到文档目录:
NSLog(@保存文件。 ..);
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@http://www.reddexuk.com/logo.png]];
AFHTTPRequestOperation * operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@logo.png];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@成功下载文件到%@,路径);
}失败:^(AFHTTPRequestOperation *操作,NSError *错误){
NSLog(@错误:%@,错误);
}];
[操作开始];
但是,我希望在成功保存后将每个文件添加到UITableView。当点击UITableView中的文件时,我想要一个UIWebView导航到该文件(所有离线)。
另外 - 我怎样才能获得文件名和结尾,例如logo.png而不是 http://www.reddexuk.com/logo.png ?
我该怎么做?<这是我用来获取目录内容的方法。
- (NSArray *)listFileAtPath:(NSString *)path
{
// ----->列出所有文件< ----- //
NSLog(@列出所有文件);
int count;
NSArray * directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for(count = 0; count<(int)[directoryContent count]; count ++)
{
NSLog(@File%d:%@,(count + 1), [directoryContent objectAtIndex:count]);
}
返回directoryContent;
}
I have set up the following code to save a file to the documents directory:
NSLog(@"Saving File...");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reddexuk.com/logo.png"]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo.png"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
However, I wish to add each file to a UITableView when it is successfully saved. When the file in the UITableView is tapped, I'd like a UIWebView to navigate to that file (all offline).
Also - how can I just get the filename and ending such as "logo.png" instead of http://www.reddexuk.com/logo.png?
How can I do this?
解决方案
Here is the method I use to get the content of a directory.
-(NSArray *)listFileAtPath:(NSString *)path
{
//-----> LIST ALL FILES <-----//
NSLog(@"LISTING ALL FILES FOUND");
int count;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (count = 0; count < (int)[directoryContent count]; count++)
{
NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
}
return directoryContent;
}
这篇关于在UITableView的iOS文档目录中列出已保存的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文