Ios 登录后重新加载数据

Ios 登录后重新加载数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个由AMSlideMenu库支持的左幻灯片菜单,它显示带有菜单项的表格视图。 amslidementulefttableviewcontroller.m - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc]initWithSt

我有一个由AMSlideMenu库支持的左幻灯片菜单,它显示带有菜单项的表格视图。

amslidementulefttableviewcontroller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;    

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"loggedUser"] != nil) {
        if (indexPath.row == 0) { cell.textLabel.text = [NSString stringWithFormat:@"Hi, %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"loggedUser"]]; }
        if (indexPath.row == 1) { cell.textLabel.text = @"Contact"; }

    } else {

        if (indexPath.row == 0) { cell.textLabel.text = @"Log in"; }
        if (indexPath.row == 1) { cell.textLabel.text = @"Contact"; }

    }
}
- (IBAction)loginButtonPressed:(id)sender {    
    if(![self.usernameTextField.text  isEqual: @""] && ![self.passwordTextField.text isEqual:@""]){

        for (UITextField *eachTextfield in self.view.subviews)
            [eachTextfield resignFirstResponder];

            PFQuery *query = [PFQuery queryWithClassName:@"UsersClass"];
            [query whereKey:@"Username" equalTo:self.usernameTextField.text];
            [query whereKey:@"Password" equalTo:self.passwordTextField.text];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {
                    // The find succeeded.
                    if (objects.count > 0){

                        [self dismissViewControllerAnimated:YES completion:nil];

                        //Get the username and save it as "loggedUser" for later use
                        [[NSUserDefaults standardUserDefaults] setObject:self.usernameTextField.text forKey:@"loggedUser"];
                        [[NSUserDefaults standardUserDefaults] synchronize];

                        [self performSegueWithIdentifier:@"showDetail" sender:self];   

                    }else{

                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:NSLocalizedString(@"The username or password are incorrect", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                        [alert show];
                    }

                } else {

                    // Log details of the failure
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];

    }else{

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:NSLocalizedString(@"Both fields are required", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }

LoginViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;    

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"loggedUser"] != nil) {
        if (indexPath.row == 0) { cell.textLabel.text = [NSString stringWithFormat:@"Hi, %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"loggedUser"]]; }
        if (indexPath.row == 1) { cell.textLabel.text = @"Contact"; }

    } else {

        if (indexPath.row == 0) { cell.textLabel.text = @"Log in"; }
        if (indexPath.row == 1) { cell.textLabel.text = @"Contact"; }

    }
}
- (IBAction)loginButtonPressed:(id)sender {    
    if(![self.usernameTextField.text  isEqual: @""] && ![self.passwordTextField.text isEqual:@""]){

        for (UITextField *eachTextfield in self.view.subviews)
            [eachTextfield resignFirstResponder];

            PFQuery *query = [PFQuery queryWithClassName:@"UsersClass"];
            [query whereKey:@"Username" equalTo:self.usernameTextField.text];
            [query whereKey:@"Password" equalTo:self.passwordTextField.text];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {
                    // The find succeeded.
                    if (objects.count > 0){

                        [self dismissViewControllerAnimated:YES completion:nil];

                        //Get the username and save it as "loggedUser" for later use
                        [[NSUserDefaults standardUserDefaults] setObject:self.usernameTextField.text forKey:@"loggedUser"];
                        [[NSUserDefaults standardUserDefaults] synchronize];

                        [self performSegueWithIdentifier:@"showDetail" sender:self];   

                    }else{

                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:NSLocalizedString(@"The username or password are incorrect", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                        [alert show];
                    }

                } else {

                    // Log details of the failure
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];

    }else{

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:NSLocalizedString(@"Both fields are required", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
但是,登录后,菜单不会刷新,并且会一直显示错误的
单元格.textlab.text
。如果我关闭并再次打开应用程序,它会起作用,但显然必须有另一种方法来解决这个问题

我尝试了
[tableView reloadData]
,但这不起作用。我已经在
viewDidLoad
上试用过,并且
viewwillbeen
没有成功


谢谢你的帮助。谢谢

以下是您需要做的事情。在您的
loginButtonPressed
方法中,如果成功登录,请发布如下通知:

NSNotification *loginNotification = [NSNotification notificationWithName:@"USER_DID_LOGIN" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:loginNotification];
在具有tableView的视图控制器中,执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateOnLogin:) name:@"USER_DID_LOGIN" object:nil];
}

- (void)updateOnLogin:(NSNotification*)notification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

每当用户成功登录时,视图控制器将收到通知,并重新加载tableView。

显示创建单元格的完整代码以及登录/写入NSUserDefaults的位置,[tableView reloadData]确实工作,可能您没有调用[synchronize]在调用reload之前在NSUserDefaults上?显示您实际设置loggedUser:的位置)尝试设置断点,以确保您的NSUserDefaults条件按预期运行。@GrzegorzKrukowski-我添加了整个代码。顺便说一句,我正在使用Parse.com进行登录验证。
NSUserDefaults
正在正确保存对象,因为如果我关闭应用程序并再次加载,菜单将正确显示。我很确定问题出在视图的重新加载上,因为在第一次加载tableView之后,
NSUserDefaults
上的对象正在更改。你搞定了!这非常有效。非常感谢你。顺便说一句,我不知道NSNotificationCenter,它是关于什么的?引用:“NSNotificationCenter对象(或者简单地说,通知中心)提供了在节目中广播信息的机制。”链接: