Sam Spencer
Sam Spencer

Reputation: 8609

Complete FIle List in Table view

The Situation: I am trying to display a list of files from my app's Documents Directory (iOS) in a UITableView.

The Problem: When the view loads, instead of listing all the files, it only lists one file (the first one alphabetically)

The Code:

cell.textLabel.text = [NSString  stringWithFormat:@"Cell Row #%d", [indexPath row]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.txt'"];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSArray *FileList = [fileListAct filteredArrayUsingPredicate:filter];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[FileList objectAtIndex:indexPath.row]];
NSLog(@"File List: %@", FileList);

All of the code performs as it should, and even the last NSLog line lists ALL of the file names, but for some reason in the UiTableView it only lists the first file name.

More Info: I've tried creating a do loop for the last cell.textlabel.text line, but that also requires a while statement (and I couldn't think of what the condition would be).

Any ideas on how to make the UITableView display all file names vs. just the first one?

Upvotes: 0

Views: 1138

Answers (1)

WrightsCS
WrightsCS

Reputation: 50697

You need to setup a global NSArray for fileList. The you need to create the array in either viewDidLoad or viewWillAppear:

Example

This is a rough example, and how I would do it, although it hasn't been tested, it should work.

@interface MyViewController () {
    NSMutableArray *FileList;
}
@end

@implementation MyViewController

- (void)viewDidLoad:(BOOL)animated
{ 
    [super viewDidLoad];

    FileList = [[NSMutableArray alloc] init];
}

/*  Setup the array here  */
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.txt'"];
    NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

    FileList = [fileListAct filteredArrayUsingPredicate:filter];
}

/* Set the number of cells based on the number of entries in your array  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [FileList count];
    /*  this is probably what you are missing and is definitely 
        the reason you are only seeing 1 cell.  */
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[FileList objectAtIndex:indexPath.row]];
}

@end

Upvotes: 2

Related Questions