[英]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.