我有一个由 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]
但这不起作用。我已经在 viewDidLoad
和 viewWillAppear
上尝试过,但没有成功。
感谢任何帮助。谢谢
最佳答案
这是您需要做的。如果成功登录,请在您的 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/