我的項目工作文件,當我下載並列出保存的文件到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