2011-02-03 76 views
0

所以,我有一個UITableView顯示應用程序Documents文件夾的內容。在該文件夾中,我有9個文本文件,稱爲1.txt,2.txt,3.txt等。我設法得到選定的行,但現在我需要加載與選定文件相對應的txt。例如,如果我觸摸2.txt,詳細視圖應打開2.txt文件上的內容。這是我無法工作的部分。感謝提前:)當UITableView被挖掘時加載一個文本文件

+0

你到目前爲止(代碼)有什麼? – aqua 2011-02-03 18:32:54

回答

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; 

而現在f ile正在顯示:D 非常感謝!

1

當您選擇該行的表視圖的委託方法被稱爲:

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

這種方法,你可以用這種方式建立自己的文件名中:


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

通知我如何使用indexPath.row(即:選定單元格的行號)構建文件名。我想在這個例子中,第一行(索引爲0)導致文件名爲1.txt

現在你可以加載這個文件。

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

(您想使用的文件夾,如果在運行時創建的文本文件,以及一個NSBundle方法,如果文件被打包在viewDidLoad方法的應用程序)在detailViewController

那麼你會把這樣的:

detailLabel.text = self.detailString; 

凡detailLabel是一個UILabel或UITextFi這取決於你是否希望它是可編輯的。

+0

所以我混合了這兩個示例,並設法查看NSLog上的文本內容,但未查看DetailViewController。 – pmerino 2011-02-03 18:57:05

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