jwknz
jwknz

Reputation: 6822

Display files from Documents Directory in UITableView - iOS5 minimum

I am trying to setup a UITableView that displays the data from the Documents Directory.

I am a bit lost for code, as I have tried many examples from Google and forums etc.

I am creating my app without Storyboard, so it is all in code.

I have got the UITableView displayed, so the delegates and DataView are set - I just need the content.

I have this code to show you, but it is not showing any data:

- (void)viewDidLoad
  {
    [super viewDidLoad];

    _firstViewWithOutNavBar = [[UIView alloc] init];
    _firstViewWithOutNavBar.frame = CGRectMake(self.view.frame.origin.x, 0, self.v  iew.frame.size.width, self.view.frame.size.height);
    _firstViewWithOutNavBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_firstViewWithOutNavBar];

    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = CGRectMake(self.view.frame.origin.x, 0,     self.view.frame.size.width, self.view.frame.size.height);
    tableView.delegate = self;
    tableView.dataSource = self;
    [_firstViewWithOutNavBar addSubview:tableView];
  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
    //alloc and init view with custom init method that accepts NSString* argument
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:indexPath.row];
    NSString*pathToPass = [documentsDirectory stringByAppendingPathComponent:
                       [tableView cellForRowAtIndexPath:indexPath].textLabel.text]; //pass this.

    NSLog(@"%@", pathToPass);

//_nsarray = [[NSArray alloc] initWithContentsOfFile:pathToPass];


  }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      return [_nsarray count];
      NSLog(@"%@", _nsarray);
   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
   {
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

      cell.textLabel.text = [NSString stringWithFormat:@"%@",[_nsarray  objectAtIndex:indexPath.row]];

      return cell;
        }

Any help would be great.

Upvotes: 1

Views: 1151

Answers (2)

prakhar
prakhar

Reputation: 828

Did you place the breakpoints in numberOfRowsInSection method to check whether this method has been invoked or not. As I can see in your code you don't have initialize your _nsarray and didn't add any object in that array. So basically your array contains 0 objects so no row will be created. In your numberOfRowsInSection method You have placed the nslog after the return statement and this will never be executed please place it before the return statement so that you can actually see the array values. I hope this will help you.

Upvotes: 1

Martin R
Martin R

Reputation: 539755

The array _nsarray which is used as table view data source in numberOfRowsInSection and cellForRowAtIndexPath, is never initialized in your code. You should do something like

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
_nsarray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL];

in viewDidLoad.

Upvotes: 1

Related Questions