2

我有一些文件保存在我的 NSDocumentsDirectory 中,我如何获取这些文件的名称,然后在 uitableview 中显示这些名称?

我只想将对象的名称检索为 NSString,我可以将文件检索为对象,但不能检索它们的名称。

这是它的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSError * error;
objectsAtPathArray =  (NSMutableArray*)[[NSFileManager defaultManager]
                      contentsOfDirectoryAtPath:documentsDirectory error:&error];
[objectsAtPathArray removeObjectAtIndex:0];

最后一行是删除 .DS_Store 文件

4

3 回答 3

2

尝试这个,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    objectsAtPathArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy];

    for(int i = 0; i < [objectsAtPathArray count]; i++)
    {
       if (![[objectsAtPathArray objectAtIndex:i]isEqualToString:@".DS_Store"])
       {
         [NewArray addObject:[objectsAtPathArray objectAtIndex:i]];
          NSLog(@"NewArray=%@",[NewArray objectAtIndex:i]);
       }
     }
于 2012-08-16T07:17:46.737 回答
0

添加一个(for in)循环对我有用,

for (NSString *fileName in self.objectsAtPathArray){

   cell.textLabel.text = fileName;
} 
于 2012-08-16T14:33:49.960 回答
0

这将正确工作:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (int i = 0; i < [imageFilenames count]; i++)
    {

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];

        if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
        {
          UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
          UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
        }
    }
于 2013-11-19T03:51:37.170 回答