Iphone 如何在我的文档目录中加载带有已保存文件列表的UITableView?
如何在我的文档目录中加载带有已保存文件列表的UITableView?我只是需要一个小例子或一个简单的在线教程,因为我一直在拔我的头发。所有文件都保存到iOS文档目录中,我需要做的就是在我的UITableView中列出它们。您可以使用Iphone 如何在我的文档目录中加载带有已保存文件列表的UITableView?,iphone,objective-c,ios,xcode,uitableview,Iphone,Objective C,Ios,Xcode,Uitableview,如何在我的文档目录中加载带有已保存文件列表的UITableView?我只是需要一个小例子或一个简单的在线教程,因为我一直在拔我的头发。所有文件都保存到iOS文档目录中,我需要做的就是在我的UITableView中列出它们。您可以使用 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *documentsDirectory = [path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
您将使用获取其中的文件列表
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
此数组将包含目录和文件。枚举dirContents并检查它是否是使用
- (BOOL)fileExistsAtPath:(NSString *)path
如果文件不存在,请从目录内容中筛选出该文件,或者创建一个新数组,其中只包含确实存在的文件的路径,并将其用作表视图的数据源。您可以使用
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
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]];
}
您将使用获取其中的文件列表
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
此数组将包含目录和文件。枚举dirContents并检查它是否是使用
- (BOOL)fileExistsAtPath:(NSString *)path
如果文件不存在,请从目录内容中筛选出该文件,或者创建一个新数组,其中只包含确实存在的文件的路径,并将其用作表视图的数据源。从上述两个答案中,您知道如何从文档目录检索文件
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]];
}
现在你想知道当用户点击te单元时如何打开一个文件
您只需使用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]]
}
现在,您的myURL具有存储在文件中的链接,然后您可以在Web视图中使用该url。从以上两个答案中,您知道如何从文档目录检索文件 现在你想知道当用户点击te单元时如何打开一个文件 您只需使用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]]
}
现在,您的myURL具有存储在文件中的链接,然后您可以在Web视图中使用该url。谢谢!真的很有帮助。点击行时,我现在如何在UIWebView中打开文件?谢谢!真的很有帮助。当点击该行时,我现在如何在UIWebView中打开该文件?同样,非常有用-谢谢!当点击该行时,我现在如何在UIWebView中打开该文件?同样,非常有用-谢谢!当点击某一行时,我现在如何在UIWebView中打开该文件?我理解这一点,但如何在点击某一行时打开某一文件?你能举个小例子吗?我明白,但当点击某一行时,我如何打开某一文件?你能举个小例子吗?