reloadData 没有调用 cellForRowAtIndexPath [英] reloadData not calling cellForRowAtIndexPath

查看:39
本文介绍了reloadData 没有调用 cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

...检查用户名信息是否有效.如果是,则在主视图中执行 segue.验证用户名方法如下所示:

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

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

和 [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");
}

我知道正在调用此方法,因此重新调用 numberOfRowsInSection.

I know this method is being called, and as a result re-calling numberOfRowsInSection.

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:

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 是.

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.

控制台日志:

numberOfRowsInSection returns 0

numberOfRowsInSection returns 6

loadLoginData

Login Successful!

推荐答案

如果 reloadData 没有调用 cellForRowAtIndexPath,那么你还没有连接上数据源未与正确的表对话.

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

这篇关于reloadData 没有调用 cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆