iPhone 表视图列表文件名



我的应用程序中有一个名为"文件"的文件夹。 我想在表格视图中显示所有这些TXT文件,然后能够单击每个文件以在不同的控制器中打开它。 我有它的所有功能,但问题在于表视图上列出的名称。 它给出了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 字符串的名称?

您应该在 cellForRowAtIndexpath: 方法中转换文件名。

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

这样,每次呈现单元格时,它都会访问数组中的正确对象并根据需要操作值。

最新更新