Load TableView With Included Files

Discussion in 'iOS Programming' started by newtoiphonesdk, Apr 7, 2012.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have several ppt slides included with the app I am currently working on. I would like for the TableView to list all of those ppt files, but am running into an issue. The NSArray always stays empty. Here is what I have in the viewDidLoad
    Code:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    	NSFileManager *manager = [NSFileManager defaultManager];
    	self.title = @"Songs";
    	self.files = [[[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] retain];
    	
    	
    	
    	self.navigationItem.rightBarButtonItem = self.editButtonItem;
    	[self.tableView reloadData];
        [super viewDidLoad];
        [super viewWillAppear:animated];
    
    If, NSUserDirectory is not where files are stored that the developer adds in, what is?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Yes, I have searched that. If you know the answer for how to return an Array of Files that I include in the mainBundle, then please answer the question. Don't waste time making a post that tells me to search, when I have already searched. "O GEE GOLLY! I can search on this here internet thingy?! I Never knew that...thank you for the useful post!"
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Perhaps [NSBundle mainBundle], possibly modified by pathForResource:xx:?

    I honestly can't figure out what answer you're looking for.

    First, the code uses NSUserDirectory, but has no comments or other explanation about why that's the correct directory. And since it's not working, apparently it isn't the correct directory.

    Second, the code has a variable named documentsDirectoryPath, which made me think "Maybe you want the documents directory", rather than NSUserDirectory.

    Third, you write "where files are stored that the developer adds in", which seems to be a description of what you want, but it's too brief and ambiguous for me to know exactly how to answer. It's ambiguous because you don't describe what you mean by "the developer adds in". Do you mean "adds to the app after it's installed"? That's probably the documents directory. Or do you mean "adds to the project, and copies to the main bundle at compile-time", which would be the main bundle directory.

    Finally, you eventually mention mainBundle, so that's probably where you should start looking. But I'm still guessing, because I still don't know exactly what you're looking for.

    If you don't know where files that are part of your build process are being placed in the main bundle, you should stop and look in your built app's main bundle for where those files are being placed. If you don't see them anywhere, then maybe they aren't being copied in the build, and that's why they don't appear.
     
  5. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #5
    Only a guess, but try this instead.

    Code:
    NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    Also you can get the app's sandbox path like this.

    Code:
    NSString *sandboxPath = NSHomeDirectory();
    
    // and append a document directory off the home directory
    
    NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];
    
    But this second way is not recommended, as Apple might change the name of the Documents folder in later releases of the operating system.

    Hope this is of some help.

    Regards Mark
     
  6. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #6
    I have a group "Resources" in my MainBundle. In that group, I store 100 different powerpoint files. I want the TableView to display those. All I need is to get the array set to return the ppt files in Resources.
     
  7. roeik macrumors member

    Joined:
    Dec 25, 2008
    #7
    You should search all the files on the main bundle. If you can't you can always copy all of the ppt files to the document directory on startup.
     
  8. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #8
    Here is what I'm trying now, where self.files is an NSArray
    Code:
      NSBundle *bundle = [NSBundle mainBundle];
       NSArray *paths  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"Resources"];
    	NSFileManager *manager = [NSFileManager defaultManager];
    	self.title = @"Devo Songs";
    	self.files = [[[manager contentsOfDirectoryAtPath:paths error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] retain];
    
    The issue is with that last line of code, Incompatible pointer types sending NSArray to parameter of type NSString.

    I'm not for sure how to get this to all work together. I've loaded TableViews form documents before, but it has always been from NSUserDocument stuff.
     
  9. newtoiphonesdk, Apr 9, 2012
    Last edited: Apr 9, 2012

    newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #9
    I have finally got it to add the files to the Table View using:
    Code:
     NSBundle *bundle = [NSBundle mainBundle];
        
        NSArray *paths  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"powerpoint"];
        NSLog(@"%@", paths);
    	self.title = @"Devo Songs";
    	self.files = paths;	
    The issue I now face is with the cell text. I use:
    Code:
    	cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
    
    But this is giving me the entire path of /Users/name/XCODE/Applications/.../.../song.ppt
    What would be the better way to get ONLY the file name, without the entire path?

    UPDATE:
    I know I could use
    Code:
            NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    
    self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
    But, that would only give me the first item in the TableView, and am having a hard time figuring out getting NSString to NSArray and everything. Array can't do lastPathComponent, and NSString won't respond to indexPath.row, so I'm a bit lost.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Break it down. Think it through.

    You seem to be flailing, trying random things that might lead to a solution. Instead of that, try thinking about exactly what a solution would look like. Then work backwards from that, step by step.

    You've already stated what the solution will look like: only the file name, without the entire path, stored in an array of file names. So what is a class that has the ability to scan a directory, returning an array that contains the pathnames? You already used it in an earlier post.

    Next, given a full pathname in an NSString, how do you break it down into pathname components? Or maybe just return the last path component? So now iterate over the array of full pathnames, replacing each full pathname with its last path component. Or just make a new array containing only the last path components. You now have an array of just file names.

    Next step back, how did you tell the scanning class which directory to scan? You give it the path of the directory. Not the path of the files in the directory, but the path of the directory itself. So now think about how to get the path of a directory located in the main bundle. Again, you've already used the class, you just need to find the right method.
     

Share This Page