2012-04-09 47 views
0

我在我的應用程序名爲「文件」的文件夾。我想顯示所有TXT文件中的一個表視圖,然後可以點擊每個人在不同的控制器來打開它。我有所有的功能,但問題出在Table View列出的名字上。它提供了TXT文件的整個工作路徑,而我只需要文件名。我知道的NSString可以使用的代碼lastComponentPath線返回,但細胞的文字不能來自一個NSString。那麼,如何讓它返回文件的名稱,並在Table View中正確列出?這裏是我當前的代碼:iPhone表視圖列表文件名?

NSBundle *bundle = [NSBundle mainBundle]; 
self.files = [bundle pathsForResourcesOfType:@"txt" inDirectory:@"Files"]; 
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row]; 
self.title = @"My Files"; 
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension]; 

對於單元格內容是:

cell.textLabel.text = [self.files objectAtIndex:indexPath.row]; 

所以,我試圖把在self.filenames但一個NSString將不indexPath.row迴應。那麼,我怎樣才能從self.files數組中加載文件,但是顯示來自self.filenames字符串的名字呢?

回答

1

你應該做文件名的轉換在cellForRowAtIndexpath:方法。

NSString *filename = [[[self.files objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension]; 
cell.textLabel.text = filename; 

這樣,每次渲染單元格時,它都會訪問數組中正確的對象,並根據您的需要操作值。

+0

那得很完美......不能相信我沒有想到的是較早的,只是太燒壞了,我猜。 – user717452 2012-04-09 19:34:47