2012-04-09 44 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