所以,我有一個UITableView顯示應用程序Documents文件夾的內容。在該文件夾中,我有9個文本文件,稱爲1.txt,2.txt,3.txt等。我設法得到選定的行,但現在我需要加載與選定文件相對應的txt。例如,如果我觸摸2.txt,詳細視圖應打開2.txt文件上的內容。這是我無法工作的部分。感謝提前:)當UITableView被挖掘時加載一個文本文件
0
A
回答
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];
}
相關問題
- 1. 當UITextField被挖掘時加載一個筆尖文件作爲一個自定義鍵盤
- 2. 中文文本挖掘
- 3. 當UITableViewCell被挖掘時UIView消失
- 4. 文本挖掘中的R
- 5. stemDocument [R文本挖掘
- 6. 電子郵件的文本挖掘
- 7. 挖掘維基百科映射文本挖掘關係
- 8. 移動UIView基於哪個文本字段已被挖掘
- 9. Swift - 當UIButton被挖掘時更改文本的顏色和大小
- 10. R的文本挖掘軟件包...添加一個新的函數getTransformation
- 11. 從CSV文件中獲取R文本挖掘文檔
- 12. Java開源文本挖掘框架
- 13. R文本挖掘 - 處理複數
- 14. R文本挖掘和隨機森林
- 15. 使用Python進行文本挖掘
- 16. UIAlertView如果CCMenuItemFont(文本)挖掘
- 17. 從文本中挖掘位置
- 18. 文本挖掘中的矩陣控制
- 19. BeautifulSoup文本挖掘 - 變量字符串
- 20. 如何文本挖掘特定數據
- 21. 使用PHP進行文本挖掘
- 22. 文本挖掘與斯卡拉
- 23. Kmeans聚類和文本挖掘在R
- 24. Python,文本挖掘,docx到表(CSV)
- 25. 文本挖掘和機器學習
- 26. 如何數據挖掘文本?
- 27. 文本挖掘中的功能數
- 28. 將兩列文本文檔轉換爲單行文本挖掘
- 29. 從CPP文件中挖掘函數
- 30. 需要關於文本挖掘的想法,以從大量文件中挖掘數據
你到目前爲止(代碼)有什麼? – aqua 2011-02-03 18:32:54