reloadData not calling cellForRowAtIndexPath

ios, objective-c, uitableview

Solution

If `reloadData` doesn't call `cellForRowAtIndexPath`, then you haven't linked up the datasource or are not talking to the correct table.

Problem

My project uses a Split View Controller containing a table view with a list of class periods and a main view detailing the class periods. When the app first loads up, there are no entries in the table view and the main view shows a login screen. When the user logs in, the table view is supposed to reload with text containing the title of each class period. I am unable to get the data to reload correctly. When the login button is pressed a message is sent to the App delegate... ``` - (IBAction)login:(id)sender { if([self.appDelegate validateUsername:self.usernameField.text validatePassword:self.passwordField.text]) { NSLog(@"Login Successful!"); [self performSegueWithIdentifier:@"loginSegue" sender:sender]; }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Unsuccessful :(" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } } ``` ...which checks to see if the username information is valid. If it is, a segue in the main view is performed. The validate username method looks like this: ``` - (BOOL)validateUsername:(NSString *)username validatePassword:(NSString *)password { // The "username isEqualToString" is placeholder. This message will request a login and proceed if the login is successful if([username isEqualToString:@"username"] && [password isEqualToString:@"password"]) { self.student = [SCHStudent newStudentWithName:@"Random Student"]; [self loadData]; return YES; }else { return NO; } } ``` [self loadData] loads an array of class periods to show in the table view: ``` - (void)loadData { [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher1" periodNumber:1]]; [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher2" periodNumber:2]]; [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher3" periodNumber:3]]; [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher4" periodNumber:4]]; [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher5" periodNumber:5]]; [self.student.classPeriods addObject:[SCHClassPeriod newClassPeriodWithTeacher:@"Teacher6" periodNumber:6]]; [self.tableViewController loadLoginData]; } ``` and [self.tableViewController loadLoginData] is found in the my table view controller and looks like this. THIS IS WHERE I AM STUCK. ``` - (void)loadLoginData { [self.tableView reloadData]; NSLog(@"loadLoginData"); } ``` I know this method is being called, and as a result re-calling numberOfRowsInSection. numberOfRowsInSection: ``` - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { self.classPeriods = self.appDelegate.student.classPeriods; NSLog(@"numberOfRowsInSection returns %ld", (long)self.classPeriods.count); // Return the number of rows in the section. return self.classPeriods.count; } ``` cellForRowAtIndexPath: ``` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"prototypeCell" forIndexPath:indexPath]; SCHClassPeriod *tempPeriod = [self.classPeriods objectAtIndex:indexPath.row]; NSLog(@"cellForRowAtIndexPath returns %@", tempPeriod.teacher); // Configure the cell... cell.textLabel.text = tempPeriod.teacher; return cell; } ``` The only message not being called is the cellForRowAtIndexPath. It is not originally called because there are no objects in the classPeriods array. This is intentional. However, I need cellForRowAtIndexPath to be called when reloadData is called and its not even though numberOfRowsInSection is. Sorry for the code dump. I want to make sure all the information necessary to answer this question is present. I've been working on this problem for a couple of days now and all similar questions to this seem to be specific to each project. EDIT: Console Log: ``` numberOfRowsInSection returns 0 numberOfRowsInSection returns 6 loadLoginData Login Successful! ```

Original source