Objective c 下载并将保存的文件列表到tableview

Objective c 下载并将保存的文件列表到tableview,objective-c,uitableview,Objective C,Uitableview,当我下载并将保存的文件列表到tableView时,我的项目工作文件。但当我将文件列表到tableView时,文件名有问题(见图) 这是我的手机号码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifi

当我下载并将保存的文件列表到tableView时,我的项目工作文件。但当我将文件列表到tableView时,文件名有问题(见图)

这是我的手机号码

- (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 !");
            });
        }

    });

}
A

不要使用
DocumentsDirectoryStringByAppendingString
创建单元格文本,只需使用文件名即可

B

不要硬编码保存文件名,而是使用:

NSString *fileName = [urlToDownload lastPathComponent];

有什么问题?什么东西不符合您的要求?我希望tableView只显示“filename.extension”而不是所有目录。我想下载不同扩展名的文件。例如,在我的项目中,我下载了一个PFD文件,但它是用123.pngSo保存的。为什么要使用
文档目录字符串通过附加字符串…
我将代码编辑为NSString*theFileName=[filePathsArray objectAtIndex:indexath.row];cell.textlab.text=文件名;现在它工作了,我想下载不同扩展名的文件。例如,在我的项目中,当我下载一个PFD文件但用123.pngso保存时,我需要删除NSString*文件路径=[NSString stringWithFormat:@“%@/%@”,documentsDirectory,@“Image.png”];并添加您的代码?您需要将“Image.png”部分更改为使用
fileName
。您还应该使用
stringByAppendingPathComponent: