user717452
user717452

Reputation: 111

iPhone Table View List File Names?

I have a folder in my app named "Files". I would like to display all of those TXT Files in a Table View, and then be able to click on each one to open it up in a different Controller. I have the functionality of it all working, but the issue lies in the name listed on the Table View. It is giving the entire working path of the TXT file, while I need only the File Name. I know that NSString can use a lastComponentPath line of code to return it, but cell text cannot come from an NSString. So, how do I get it to return the name of the file, and list it properly in the Table View? Here is my current code:

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];

For the cell content it is:

cell.textLabel.text = [self.files objectAtIndex:indexPath.row];

So, I tried putting in self.filenames but an NSString won't respond to indexPath.row. So, how can I load the files from the self.files array, but display names from self.filenames string?

Upvotes: 0

Views: 808

Answers (1)

jonkroll
jonkroll

Reputation: 15722

You should do the conversion of the filename in your cellForRowAtIndexpath: method.

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

That way each time a cell is rendered it will access the correct object in your array and manipulate the value according to your needs.

Upvotes: 1

Related Questions