Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone表格视图列表文件名?_Iphone_Xcode_Nsstring_Nsarray_Tableview - Fatal编程技术网

iPhone表格视图列表文件名?

iPhone表格视图列表文件名?,iphone,xcode,nsstring,nsarray,tableview,Iphone,Xcode,Nsstring,Nsarray,Tableview,我的应用程序中有一个名为“文件”的文件夹。我想在一个表视图中显示所有这些TXT文件,然后能够单击每个文件在不同的控制器中打开它。我的所有功能都正常工作,但问题在于表视图中列出的名称。它给出了TXT文件的整个工作路径,而我只需要文件名。我知道NSString可以使用lastComponentPath代码行返回它,但单元格文本不能来自NSString。那么,如何让它返回文件名,并在表视图中正确地列出它呢?这是我目前的代码: NSBundle *bundle = [NSBundle mainBundl

我的应用程序中有一个名为“文件”的文件夹。我想在一个表视图中显示所有这些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.filename,但NSString不会响应indexPath.row。那么,如何从self.files数组加载文件,但显示self.filenames字符串中的名称呢?

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

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

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

效果非常好……真不敢相信我之前没有想到这一点,我想是太累了。