我的项目工作文件,当我下载并列出保存的文件到tableView。但是当我列出文件到tableView时(见图),我遇到了文件名的问题。下载并保存文件到tableview
这里是我的cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [documentsDirectory stringByAppendingString:[filePathsArray objectAtIndex:indexPath.row]];
return cell;
}
另一个我想下载不同的文件不同的扩展名,并与原来的文件保存它,但我的代码仅保存我设置文件名。 这里是我的代码:
- (IBAction)download {
// NSString* fileToSaveTo = @"file";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://chingfong.com/Icon.png";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if (urlData)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Image.png"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
}
这是什么问题?你不喜欢什么工作? – Wain
我想tableView只显示“filename.extension”并不是所有的目录。我想用不同的扩展名下载不同的文件。在我的项目中,当我下载一个PFD文件,但它保存与123.png –
为什么你为什么使用'documentsDirectory stringByAppendingString ...' – Wain