Reputation: 1102
I have some files saved in my NSDocumentsDirectory, how do i get the names of those files and then display those names in a uitableview?
I just want to retrieve the name of the objects as NSString, I am able to retrieve the files as objects but not their names.
Here is the code for it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError * error;
objectsAtPathArray = (NSMutableArray*)[[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsDirectory error:&error];
[objectsAtPathArray removeObjectAtIndex:0];
Last line is to remove the .DS_Store file
Upvotes: 2
Views: 364
Reputation: 11217
This will work correct:
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];
}
}
Upvotes: 0
Reputation: 1102
adding a (for in) loop did the trick for me,
for (NSString *fileName in self.objectsAtPathArray){
cell.textLabel.text = fileName;
}
Upvotes: 0
Reputation: 6490
try this,
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]);
}
}
Upvotes: 2