所以,我有一个UITableView显示app Documents文件夹的内容。在该文件夹中,我有9个文本文件,分别为1.txt,2.txt,3.txt等。我已设法获取所选行,但现在我需要加载与所选文件对应的txt。例如,如果我触摸2.txt,详细信息视图应该打开2.txt文件上的内容。这是我无法工作的部分。在此先感谢:)
答案 0 :(得分:1)
当您选择行时,将调用表视图委托方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
在此方法中,您可以通过以下方式构建文件名:
NSString *fileName = [DocumentsFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.txt",indexPath.row+1]];
注意我是如何使用indexPath.row(即所选单元格的行号)来构建文件名的。我想在示例中第一行(索引为0)导致文件名1.txt
现在您可以加载此文件。
答案 1 :(得分:0)
在didSelectRowAtIndexPath:方法中,您可以执行以下操作:
NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"]; //< or getting the file from the documents folder
NSString *fileText = [NSString stringWithContentsOfFile:path]; //< this line actually reads the text in the file at the path provided
detailViewController.detailString = fileText;
[self.navigationController pushViewController:detailViewController animated:YES];
(如果在运行时创建文本文件,则使用documents文件夹;如果文件与应用程序打包,则使用NSBundle方法)
然后在viewDidLoad方法的detailViewController中你会输入如下内容:
detailLabel.text = self.detailString;
其中detailLabel是UILabel或UITextField,具体取决于您是否希望它可编辑等。
答案 2 :(得分:0)
所以我混合了两个例子并设法在NSLog上看到文本的内容,但没有在DetailViewController上看到。 这是我用来做的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.txt",indexPath.row+1]];
NSString *fileText = [NSString stringWithContentsOfFile:fileName]; //< this line actually reads the text in the file at the path provided
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.labelName.text = NSString = [stringWithContentsOfFile:fileName];
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(@"didSelectRowAtIndexPath: row=%d", indexPath.row);
NSLog(fileText);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[detailViewController release];
}
答案 3 :(得分:0)
好的方法是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.txt",indexPath.row+1]];
NSString *fileText = [NSString stringWithContentsOfFile:fileName]; //< this line actually reads the text in the file at the path provided
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.strName = fileText;
[self.navigationController pushViewController:detailViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[detailViewController release];
}
请注意我在这里更改了:
detailViewController.labelName = fileText;
要:
detailViewController.strName = fileText;
现在文件出现了:D 非常感谢!