alvarolopez
alvarolopez

Reputation: 467

Reload data after logging in

I have a left slide menu powered by AMSlideMenu library that displays a tableview with menu items.

AMSlideMenuLeftTableViewController.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"; }

    }
}


LoginViewController.m

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

However, after logging in, the menu does not refresh and keeps displaying the wrong cell.textLabel.text. It works if I close and open again the application, but obviously there has to be another way of solving this.

I have tried [tableView reloadData] but this does not work. I have tried it on viewDidLoad and viewWillAppear without success.

Appreciate any help. Thanks

Upvotes: 0

Views: 280

Answers (1)

almas
almas

Reputation: 7187

Here is what you need to do. In your loginButtonPressed method in case of a successful login post a notification like this:

NSNotification *loginNotification = [NSNotification notificationWithName:@"USER_DID_LOGIN" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:loginNotification];

In your view controller with tableView do this:

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

Whenever user logs in successfully, your view controller will receive a notification, and it will reload the tableView.

Upvotes: 1

Related Questions