reloadData不调用cellForRowAtIndexPath

我的项目使用一个拆分视图控制器,其中包含一个包含类周期列表的表视图和一个详细说明类周期的主视图。 当应用程序首次加载时,表视图中没有条目,主视图显示登录屏幕。 当用户登录时,表视图应该使用包含每个类周期标题的文本重新加载。 我无法正确重新加载数据。

按下登录按钮后,会向App委托发送一条消息…

- (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]; } } 

…检查用户名信息是否有效。 如果是,则执行主视图中的segue。 validation用户名方法如下所示:

 - (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]加载要在表视图中显示的类周期数组:

 - (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]; } 

和[self.tableViewController loadLoginData]可以在我的表视图控制器中找到,看起来像这样。 这就是我在哪里

 - (void)loadLoginData { [self.tableView reloadData]; NSLog(@"loadLoginData"); } 

我知道正在调用此方法,因此重新调用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; } 

唯一没有被调用的消息是cellForRowAtIndexPath。 它最初未被调用,因为classPeriods数组中没有对象。 这是故意的。 但是,我需要在调用reloadData时调用cellForRowAtIndexPath,它甚至不是numberOfRowsInSection。

对不起代码转储。 我想确保回答这个问题所需的所有信息都存在。 我已经在这个问题上工作了几天,所有类似的问题似乎都是针对每个项目的。

编辑:控制台日志:

 numberOfRowsInSection returns 0 numberOfRowsInSection returns 6 loadLoginData Login Successful! 

如果reloadData没有调用cellForRowAtIndexPath,那么您没有链接数据源或者没有与正确的表进行通信。

我想出了我的。 我通过Storyboard Connections Inspector连接了所有内容 ,但教会我的教程也告诉我将其添加到类的viewDidLoad中:

 self.tableView.delegate = self; self.tableView.dataSource = self; 

当我删除这两行代码时,一切都像魅力一样!

现在我在ViewWillAppear中调用[self.tableView reloadData]方法,即使在segues中来回传递它也能正常工作。

希望这有助于任何人!

在故事板中将UItableview拖到名为tableView的属性中,这样当你调用[self.tableView reloadData] ,sockets会知道要重新加载哪个表

尝试像我这样的选择器方法重新加载数据

 -(void)viewWillAppear:(BOOL)animated{ [self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.2]; [self.tableView reloadData]; not working } - (void)reloadTable { [self.tableView reloadData]; } 

它适用于我,在我的情况下,我从导航回来并在viewwillappear方法重新加载uitableview。