ios - 登录后重新加载数据

标签 ios objective-c uitableview

我有一个由 AMSlideMenu 库提供支持的左滑菜单,它显示带有菜单项的表格 View 。

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

但是登录后菜单不刷新,一直显示错误的cell.textLabel.text。如果我关闭并再次打开应用程序,它就可以工作,但显然必须有另一种方法来解决这个问题。

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

感谢任何帮助。谢谢

最佳答案

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

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

在带有 tableView 的 View Controller 中执行此操作:

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

每当用户成功登录时,您的 View Controller 将收到通知,并重新加载 tableView。

关于ios - 登录后重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429128/

相关文章:

ios - NSPredicate 对关系属性的计数

ios - 将字符串转换为 NSDate 给出错误的日期

ios - UITableViewCell 中的 NSLayoutConstraint 中断

iphone - 重置内容插入后如何设置滚动偏移?

objective-c - 反转 NSArray 顺序的简便方法

ios - 无法存档 Xcode 项目,错误没有这样的模块 'Alamofire'

ios - 如何检测 UIScrollView 是否空闲超过 500 毫秒?

ios - 如何将下一个按钮发送到 UITableView

ios - 在 UITableView 中实现 imageView 滑动,具有水平滚动和页面指示器

ios - 如何将 UITableView 连接到新创建的 UITableView 类?