Populate table view with filenames - iphone

Is there a way to populate the data in a table view with the filenames from a certain folder?
Thanks.

NSFileManager's documentation has a section under Tasks called 'Discovering Directory Contents'. Use these methods to get the contents of a directory and then populate your data source for the table view with the results.
You can use that data to display the directory contents in the table view.

This is the code I used (for anyone that may need it):
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:bundleRoot];

Related

How to get the files inside a folder in Supporting Files in xcode 4.2?

I have done the following steps.
1. Created an xcode project and created a folder named "Images" inside Supporting Files.
Dragged and dropped 4 images into it. Tried to access those files using the following code
NSString *pathString = [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: nil];
NSLog(#"the fileList is %d",[fileList count]);
The count is always 0. What should I do now? There are files inside it and I am able to use it in imageviews.
So what is the mistake that I am making?
The Xcode does not generate any folders in your app bundle that corresponds to groups. Any resources added are present in Copy Bundle Resources in your target's Build Phases. You then access your resource under [[NSBundle mainBundle] bundlePath] path.
But if you want to count files under any folder then while adding that folder you must check the option :
"Create folder references for any added folders."
This will create folders and sub-folders in the same hierarchy as you add them.Then you can easily count them in the same manner you are doing above...
Otherwise the app bundle has all of your resources at one place not in any folder as you say "Images".Use following code :
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
NSLog(#"the fileList is %d",[fileList count]);
It will list all your resources.
Try this,
NSString *imagespath = [[NSBundle mainBundle] pathForResource:"yourimagename" ofType:#"png" inDirectory:#"images"];

changing the name of pdf by the title

In my application I obtain a title of PDF which is saved in my plist with static name which is i given like s.pdf
After that I find the title of that pdf and now I want to save the same pdf with that title name which I got and also replacing the old name which is s.pdf
So how can I do that?
You probably going to need NSFileManager copyItemAtPath:toPath:error:
Copies the directory or file specified in a given path to a different location in the file system identified by another path.
Example This is not a complete code it's for explanation only:
// This will get you the Bundle Path.
NSString*path = [[NSBundle mainBundle] resourcePath];
// This will get your PDF in the Bundle Path.
[[NSBundle mainBundle] pathForResource:#"s" ofType:#"pdf"]
// Copy your file to the same destination but with different name.
[[NSFileManager defaultManager] copyItemAtPath:OldFileNameInBundle
toPath:NewFileNameInBundle
error:&err]
NSFileManager copyItemAtPath

Why can't files in my iPhone NSBundle folder be deleted?

I'm having trouble figuring out why files in my iPhone app seem to persist, even when I've deleted them. Here's the code that's giving me trouble:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *folderPath = [bundlePath stringByAppendingPathComponent:#"filefolder"];
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:folderPath error:NULL];
This code is supposed to look at the folder "filefolder" and read its contents into fileNames. When I run this app the very first time, it will do this. But if I change the contents of filefolder (for instance if I add or delete files) and I build and run the app again, the array filenames will contain names of all the newly added files (good) but also contains names of all the files that were supposed to have been deleted (bad)!!
Can anyone help me understand why I'm seeing this behavior?
Did you do a "Clean" in XCode after adding/removing new items from the bundle? This usually solves the problem of "stale" resources.

Getting Images List using bundle

I am using this code to get the list of Images in my project
NSArray *pngPaths = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil];
I dont want to get one image from the list can i add filter to it
Thank You
I think it is easier to do when you already load all paths into your memory NSArray. You can just loop over all elements and examine the path and remove paths you don't want

Is possible to read plist from application bundle and documents folder at the same time?

Is it possible?to read from my local bundle and at the same time also read from documents folder into UItableview?
thanks thanks
yes.simultaneously
No — as in the iPhone isn't multicore, you can't have "simultaneous" :p
Yes — as in you can open multiple files in the same period of time. There's no conflicts as long as the files are different (if the files are the same then it depends on how others are using and locking the file etc.)
on viewDidLoad or some similar event when you would be populating your table data, you would simply just aggregate the two files together... that is you are are likely populating an array or dictionary with the contents of the file in question... so use the mutable version of array/dictionary, initialize it empty, then read in the first file from whatever location you choose, populating into your mutable array/dictionary, then do the the same for the next file. after you are done, reloadData as you normally would as if you had read form one file.
As far as simultaneous goes, technically no. However, one could have two different active threads each one reading required files and parsing the data.
Regarding the files you want to access...
Here is a quick and dirty method I use in one project (which I just happen to be working on at the moment):
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError* error;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
self.blahDBPath = [self.documentsDirectory stringByAppendingPathComponent: #"blah.db"];
NSLog(#"Mainbundle Resourcepath: %#", [[NSBundle mainBundle] resourcePath]);
NSString* defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"blah.db"];
NSLog(#"Default DB Path: %#", defaultDBPath);
success = [fileManager copyItemAtPath:defaultDBPath toPath:self.blahDBPath error:&error];
if (!success) {
NSAssert1(0, #"blah blah blah '%#'.", [error localizedDescription]);
}
It is ugly but effective. I'll rewrite it when I refactor the application.
The point here is that I ask the operating system for the path to certain directories. Then I add file names or subdirectories as required. This allows the operating system to manage paths (like in the simulator where each successive build gets a new unique id as part of its path) and I just worry about the final directories and file names in the application.
One I have the paths, I copy the required file from the bundle directory and put them somewhere, the Documents directory in this case. Then I can do whatever I need to with them.
If I just wanted to access them as they are in the bundle directory, then I I just refer to them by using [[NSBundle mainBundle] resourcePath].
I think something along the lines of the above snippet is what you are looking for.
-isdi-