轻按UITableView时加载文本文件 [英] Load a text file when UITableView is tapped
问题描述
因此,我有一个UITableView显示了应用程序Documents文件夹的内容.在该文件夹中,我有9个文本文件,分别称为1.txt,2.txt,3.txt等.我已经设法获得了选定的行,但是现在我需要加载与选定文件相对应的txt.例如,如果我触摸2.txt,则详细视图应打开2.txt文件上的内容.那是我无法开始工作的部分.在此先感谢:)
So, I've got an UITableView showing the contents of the app Documents folder. In that folder I have 9 text files called 1.txt, 2.txt, 3.txt and so. I've managed to get the selected row, but now I need to load the txt wich corresponds to the selected file. By example if I touch the 2.txt, the detail view should open what is on 2.txt file. That's the part I don't manage to get working. Thanks in advance :)
推荐答案
好的方法是:
- (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];
}
请注意,我在这里进行了更改:
Notice that I changed here:
detailViewController.labelName = fileText;
收件人:
detailViewController.strName = fileText;
现在文件显示出来了:D非常感谢!
And now the file is showing up :D So much thanks!
这篇关于轻按UITableView时加载文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!