Question

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.

Was it helpful?

Solution

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> {

 }

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top