简体   繁体   English

如何在文档目录中加载带有保存文件列表的UITableView?

[英]How can I load my UITableView with a list of saved files in my documents directory?

How can I load my UITableView with a list of saved files in my documents directory? 如何在文档目录中加载带有保存文件列表的UITableView? I just need a small example or a simple online tutorial as I've been pulling my hair out. 我一直在努力,我只需要一个小例子或一个简单的在线教程。 All the files are saved to the iOS document directory and all I need to do is list them in my UITableView. 所有文件都保存到iOS文档目录中,我要做的就是在UITableView中列出它们。

NSArray *filePathsArray;

- (void)setUpTheFileNamesToBeListed
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [filePathsArray count];   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    //Insert this line to add the file name to the list
    cell.textLabel.text = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
}

You can get the path of the documents directory using 您可以使用获取目录的路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

You will get the list of files in it using 您将使用来获取其中的文件列表

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

This array will contain directories as well as files. 该数组将包含目录和文件。 Enumerate through dirContents and check if it is a file using 通过dirContents枚举并使用以下命令检查它是否为文件

- (BOOL)fileExistsAtPath:(NSString *)path

If the file does not exist, either filter that out from the dirContents or else create a new array with paths of only the files that do exist and use that as a datasource for the table view. 如果文件不存在,则从dirContents过滤掉该文件,或者创建一个仅包含确实存在文件的路径的新数组,并将其用作表视图的数据源。

From the above two answers you know how to retrieve the files from the document directory. 从以上两个答案中,您知道如何从文档目录中检索文件。

and Now you want how to open a file when a user tap on te cell. 现在,您要如何在用户点击单元格时打开文件。

you just have to use the UITableView delegate function ie 您只需要使用UITableView委托函数即

       -(void)tableView(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
        {
               //do all your file opening logic here.
               //your file data is stored in the NSArray myFileData(refer the previous answer posted above)
              //and suppose you want to fetch the data which is in the file, is of type NSURL, means the file data is just a URL link.
        //just declare a NSURL in your .h(say myURL) (synthesize it etc)


        myURL=[NSURL URLWithString:[myFileData objectAtIndex:indexPath.row]]
         }

now your myURL is having the link which is stored in the file and then you can use that url in your webView. 现在,您的myURL拥有存储在文件中的链接,然后您可以在webView中使用该URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 UITableView 中列出 iOS 文档目录中保存的文件? - List saved files in iOS documents directory in a UITableView? 如何在UIWebView中打开我的documentDirectory(来自我的UITableView)文件? - How can I open my documentDirectory (from my UITableView) files in a UIWebView? 我如何只读取保存在文档目录中的png文件并将其添加到数组? - How do i read only png files saved in Documents Directory and add it toan Array? 如何从其他iOS应用程序读取应用程序文档目录中的文档? - How can I read documents in my app's Document directory from other iOS apps? iPhone编程:我可以在应用程序的文档目录(根文件夹)中存储多少数据? - iPhone programming: How much data can I store in my app's Documents Directory (root folder)? 如何获取保存在文档目录中的CSV文件的列名? - How to get column names of CSV files saved in documents directory? 如何删除我的应用程序文档目录中的文件? - How do I delete a file in my apps documents directory? 如何通过iTunes将文件复制到我的应用程序的“文档”文件夹中 - How can I copy files to Documents folder of my app through iTunes 如何在iPhone的Documents文件夹中找到文件的文件名? - How do I find filenames of files in my iPhones Documents folder? 我无法加载存储在我的文档目录中的数据 - I can't load the data which stored in my document directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM