tarheel
tarheel

Reputation: 4797

Why isn't my tableView loading?

I have the following code, where I believe the NSFetchRequest is in fact working, but there is nothing showing in my tableView(peopleList) after the viewWillAppear runs.

-(void)viewWillAppear:(BOOL)animated{

  AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *moc = [appDelegate managedObjectContext]; 

  NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" 
                                                     inManagedObjectContext:moc];

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:entityDescription];


  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
  [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];


  NSError *error = nil;

  NSLog(@"Count for request is %a", [moc countForFetchRequest:request error:&error]);

  personArray = [moc executeFetchRequest:request error:&error];

  [peopleList reloadData];
}

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

  static NSString *cellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier];
  }

  NSString *cellValue = [personArray objectAtIndex:indexPath.row];
  cell.textLabel.text = cellValue;

  return cell;

}

Just in case, my storyboard is hooked up as shown.

enter image description here

My console says "Count for request is 23", making me think that the breakdown is somewhere in getting the array itself to show up in the interface. What needs to happen to get my personArray to show up in peopleList?

Any help is appreciated.

Upvotes: 0

Views: 574

Answers (2)

Rayfleck
Rayfleck

Reputation: 12106

You need to declare a UITableViewDataSource, which is an object that implements that protocol and supplies data to your table. This is the piece that calls cellForRowAtIndexPath. It is frequently self -i.e. the tableViewController class, because it usually has the array as a local variable.

myTableView.dataSource = self;

And in your header file, do something like this:

 @interface myTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {

 }

Upvotes: 1

sosborn
sosborn

Reputation: 14694

If you fetch is working, then I would guess that you haven't set the datasource and/or delegate for the tableview.

Upvotes: 0

Related Questions