views:

403

answers:

2

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero     reuseIdentifier:CellIdentifier] autorelease];
 cell.hidesAccessoryWhenEditing = YES;
 }

cell.accessoryType = UITableViewCellAccessoryNone;
//cell.textLabel.text = @"No Ringtones";
//cell.textLabel.text = @"Test";

 NSString *theFiles;
 NSFileManager *manager = [NSFileManager defaultManager];
 NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
 for (NSString *s in fileList){
    theFiles = s;
 }
cell.textLabel.text = theFiles;

return cell;

}

It loads fine, no errors, when I use NSLog(s) it lists all the files in the directory just fine. I even tried [s objectAtIndex:indexPath.row] but i get objectAtIndex error. Anyone have any ideas?

A: 

Your for loop is just iterating over the files and setting theFiles to the current path. So at the end of the loop, theFiles will just be the last string in the collection.

Try something like:

cell.textLabel.text = [fileList objectAtIndex:indexPath.row];
Alex Deem
You are semi-correct on this answer, what was actually incorrect was the NSMutableArray wich allowed the objectAtIndex.
AWright4911
objectAtIndex is provided on NSArray though is it not?
Alex Deem
A: 

I actually love asking questions on here, cause in less than 10 minutes, I answer my own question! <3.

This is how I got the above code to work:

     NSMutableArray *theFiles;
  NSFileManager *manager = [NSFileManager defaultManager];
  NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
  for (NSString *s in fileList){
   theFiles = fileList;
  }
 cell.textLabel.text = [theFiles objectAtIndex:indexPath.row];

 return cell;

I just made the NSString an NSMutableArray, and that allowed me to use the objectAtIndex. Now to trim the file extension!

AWright4911
You do realise that you're copying the array several times here, which is highly inefficient. You do not need the for loop, and I doubt you need to create a mutable copy of the array at all.
Alex Deem