0

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.

tarheel
  • 4,727
  • 9
  • 39
  • 52

2 Answers2

1

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

 }
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • I get a 'Passing 'MainScreen *const_strong' to parameter of incompatible type 'id' error. – tarheel Nov 30 '11 at 04:41
  • Is your table view controller declaring that it implements the UITableViewDataSource protocol? See my last edit. – Rayfleck Nov 30 '11 at 04:47
  • No, it wasn't, but I have corrected that. Now it is throwing an exception, and claiming the line "cell.textLabel.text = cellValue;" is the problem. Console says 'NSInvalidArgumentException', reason: '-[Person isEqualToString:]: unrecognized selector sent to instance 0x912f2c0'. – tarheel Nov 30 '11 at 05:18
  • What kind of objects are in your personArray? Are they NSString instances or some kind of Person class? If Person class, you need to do something like Person.lastName. – Rayfleck Nov 30 '11 at 05:40
  • It is a Person class that has 2 attributes firstName and lastName. – tarheel Nov 30 '11 at 23:18
  • So you need to do this: NSString *cellValue = ((Person *)[personArray objectAtIndex:indexPath.row]).lastName; // or combine first and last into a nice name string using NSString stringWithFormat – Rayfleck Nov 30 '11 at 23:52
  • Worked like a charm! Is there a specific place(book, website, tutorial....) you learned about CoreData? I clearly need to learn more about this subject. – tarheel Dec 02 '11 at 04:45
  • Apple's documentation is excellent. Go to developer.apple.com and search for "core data" - that should keep you busy for a while :-) – Rayfleck Dec 02 '11 at 13:32
0

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

sosborn
  • 14,676
  • 2
  • 42
  • 46