【发布时间】:2014-05-27 07:10:12
【问题描述】:
我有 2 个视图。第一个上面有UITableView,它可以完美加载,而且如果我点击通过didSelectRowAtIndexPath 触发的logOut 方法,它可以完美运行。
对于登录,我有另一个视图,它作为登录的模式视图加载在顶部。一旦登录返回成功,我希望它从第一个视图刷新UITableView 上的菜单。不幸的是,这不起作用。
MenuVC.h
@interface MenuVC : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *mainNavigation;
-(void)logOut;
-(void)refreshMenu;
MenuVC.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Aanwezig in viewDidLoad");
NSUserDefaults *userData=[NSUserDefaults standardUserDefaults];
self.mainNavigation.dataSource = self;
self.mainNavigation.delegate = self;
self.mainNavigation.backgroundColor = [UIColor clearColor];
self.menu = [NSMutableArray arrayWithObjects:@"Login",@"Available", @"Downloads", @"FAQ", nil];
if ( [userData objectForKey:@"userId"] != nil ) {
[self.menu removeObject:@"Login"];
[self.menu insertObject:[userData objectForKey:@"email"] atIndex:0];
[self.menu insertObject:@"Logout" atIndex:1];
signedInSuccess = YES;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"menuCell";
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell=[[MenuCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if ( signedInSuccess == YES && indexPath.row == 0 ) {
NSLog(@"Disabled: %@", [self.menu objectAtIndex:indexPath.row]);
cell.userInteractionEnabled = NO;
cell.menuTextLabel.adjustsFontSizeToFitWidth = YES;
} else {
cell.userInteractionEnabled = YES;
cell.menuTextLabel.adjustsFontSizeToFitWidth = NO;
}
cell.menuTextLabel.text = [self.menu objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Logout and refreshMenu both work here
if ( [identifier isEqualToString:@"Logout"] ) {
NSLog("Logout button clicked");
[self logOut];
[self refreshMenu];
}
//Rest of the code
}
-(void)refreshMenu {
[self viewDidLoad];
[self.mainNavigation reloadData];
}
-(void)logOut {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"email"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
signedInSuccess = NO;
}
以上所有代码都有效,只有这里发生了问题,当我点击loginClicked 并调用refreshMenu 它正在执行[self viewDidLoad],但NOT来自[self.mainNavigation reloadData]; refreshMenu 方法。
LoginVC.m
#import "MenuVC.h"
- (IBAction)loginClicked:(id)sender {
{
// If login process is success (which works)
MenuVC *MenuViewController = [[MenuVC alloc] init];
[MenuViewController refreshMenu];
}
【问题讨论】:
-
嗯,首先,你不应该调用
[self viewDidLoad];然后,你初始化了MenuVC。和上一个是同一个对象吗? -
你设置的是tableview的delegate还是数据源?
-
移除 [self viewDidLoad];从 menurefresh 方法不要在实现类中用户定义的方法中的任何地方调用 viewDidload..
-
alloc后不调用[MenuViewController refreshMenu],而是调用方法[self.mainNavigation reloadData];在 MenuVC.m 的 viewDidLoad 中
-
我添加了更多代码只是为了清除问题,希望这有助于我解决这个问题。@Larme,如果我删除它,当我点击“didSelectRowAtIndexPath -注销”不再起作用。马卡雷特,是的,我做到了。 user2071152 这似乎不起作用。
标签: ios uitableview uibutton viewdidload reloaddata