iPhone表视图列表文件名?

时间:2012-04-09 17:24:02

标签: iphone xcode nsstring nsarray tableview

我的应用中有一个名为“Files”的文件夹。我想在表视图中显示所有这些TXT文件,然后可以单击每个文件以在不同的Controller中打开它。我有它的功能都工作,但问题在于表视图中列出的名称。它给出了TXT文件的整个工作路径,而我只需要文件名。我知道NSString可以使用lastComponentPath代码行来返回它,但是单元格文本不能来自NSString。那么,我如何让它返回文件的名称,并在表视图中正确列出?这是我目前的代码:

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 个答案:

答案 0 :(得分:1)

您应该使用cellForRowAtIndexpath:方法转换文件名。

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

这样,每次渲染一个单元格时,它都会访问数组中的正确对象,并根据您的需要操作该值。

相关问题