简体   繁体   中英

Load a text file when UITableView is tapped

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 :)

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.

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM