点击 UITableView 时加载文本文件

发布于 2024-10-16 01:37:09 字数 174 浏览 2 评论 0原文

因此,我有一个 UITableView 显示应用程序文档文件夹的内容。在该文件夹中,我有 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷弦 2024-10-23 01:37:09

当您选择行时,将调用表视图委托方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

在该方法中,您可以通过以下方式构建文件名:


NSString *fileName = [DocumentsFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.txt",indexPath.row+1]];

注意我如何使用indexPath.row(即:所选单元格的行号)来构建文件名。我想在示例中第一行(索引为 0)导致文件名 1.txt

现在您可以加载此文件。

When you select the row the table view delegate method is called:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

inside this method you can build your file name in this way:


NSString *fileName = [DocumentsFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.txt",indexPath.row+1]];

notice how I used the indexPath.row (that is: row number of selected cell) to build the file name. I suppose in the example that first row (indexed with 0) leads to file name 1.txt

Now you can load this file.

绳情 2024-10-23 01:37:09

因此,我混合了这两个示例,并设法在 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];
}

So I mixed the two examples and managed to see the content of the text on NSLog, but not on DetailViewController.
Here is the code I'm using for doing it:

- (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];
}
隐诗 2024-10-23 01:37:09

好的,这样做的方法是:

    - (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
非常感谢!

Okay the way of doing it is:

    - (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;

To:

detailViewController.strName = fileText;

And now the file is showing up :D
So much thanks!

恰似旧人归 2024-10-23 01:37:09

在 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];

如果文本文件是在运行时创建的,则您将使用文档文件夹,如果文件与应用程序打包在一起,则将使用 NSBundle 方法)

然后在 viewDidLoad 方法中的detailViewController中执行以下操作你可以输入类似的内容:

detailLabel.text = self.detailString;

其中detailLabel是UILabel或UITextField,具体取决于你是否希望它可编辑等。

In the didSelectRowAtIndexPath: method you'd do something like:

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];

(you'd use the documents folder if the text files are created at runtime, and the NSBundle methods if the files are packaged with the app)

then in the detailViewController in the viewDidLoad method you'd put something like:

detailLabel.text = self.detailString;

Where detailLabel is a UILabel or UITextField, depending on whether you want it to be editable or not etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文