reloadData不调用cellForRowAtIndexPath

时间:2014-04-26 20:24:37

标签: ios objective-c uitableview

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

按下登录按钮后,会向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。 验证用户名方法如下所示:

- (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!

4 个答案:

答案 0 :(得分:5)

如果reloadData没有拨打cellForRowAtIndexPath,那么您还没有将数据源链接,或没有与正确的表格通话

答案 1 :(得分:3)

我想出了我的意思。 我通过Storyboard Connections Inspector 连接了所有内容,但是教我这个教程的教程也告诉我要将它添加到班级viewDidLoad

self.tableView.delegate = self;

self.tableView.dataSource = self;

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

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

希望这对任何人都有帮助!

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

-(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。