Reputation: 95
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!
Upvotes: 9
Views: 15432
Reputation: 9823
If reloadData
doesn't call cellForRowAtIndexPath
, then you haven't linked up the datasource or are not talking to the correct table.
Upvotes: 5
Reputation: 219
I figured mine out. I connected everything via Storyboard Connections Inspector, but the tutorial that taught me this also told me to add this to the class's viewDidLoad
:
self.tableView.delegate = self;
self.tableView.dataSource = self;
When I deleted these two lines of code, everything worked like a charm!
Now I call the [self.tableView reloadData]
method in the viewWillAppear
and it works even when going back and forth through segues.
Hope this helped anyone!
Upvotes: 3
Reputation: 45500
In the storyboard drag the from the UItableview to the property called tableView
so that when you call [self.tableView reloadData]
the outlet will know which table to reload
Upvotes: 0