0

我在我的应用程序中使用它作为侧边菜单:https ://github.com/romaonthego/RESideMenu

但是,我在让它与两个数组一起工作时遇到了问题。如果用户登录,我希望我的 RESideMenu 显示某个菜单项数组,如果没有用户,则显示不同的菜单项数组。

假设我打开了应用程序,但我没有登录,如果我打开菜单就会显示正确的菜单。但是,如果我然后登录,如果我再次打开它,菜单不会改变,它保持不变,就好像我没有登录一样。现在,如果我打开应用程序,但我没有登录,那么我登录,然后将应用程序退出多任务处理,再次打开它,现在显示“登录”菜单!

这是我的菜单视图的 .h 文件中的代码:

//
//  JSAMenuViewController.h
//  JSApp
//
//  Created by Jacob Klapper on 10/31/13.
//
//

#import <UIKit/UIKit.h>
#import "RESideMenu.h"

@interface JSAMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, RESideMenuDelegate>

@property (strong, readwrite, nonatomic) UITableView *tableView;

@property NSArray *currentArray;

@property NSArray *inTitles;

@property NSArray *outTitles;

@end

还有他们:

//
//  JSAMenuViewController.m
//  JSApp
//
//  Created by Jacob Klapper on 10/31/13.
//
//

#import "JSAMenuViewController.h"
#import "UIViewController+RESideMenu.h"
#import <Parse/Parse.h>

@interface JSAMenuViewController ()

@end

@implementation JSAMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //height 54 * 5
    //y = (self.view.frame.size.height - 54 * 5) / 2.0f
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 490) / 2.0f, self.view.frame.size.width, 490) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];

        tableView.backgroundView = nil;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.bounces = NO;
        tableView.scrollsToTop = NO;
        tableView;
    });
    [self.view addSubview:self.tableView];

    self.inTitles = [NSArray arrayWithObjects:@"Login", @"Register", @"About Us", @"Who's Who", @"Request a Position", @"My Debates", @"Submit an Idea", @"Debates", @"Logout", nil];

    self.outTitles = [NSArray arrayWithObjects:@"Login", @"Register", @"About Us", @"Who's Who", nil];

    if([PFUser currentUser])
    {
        self.currentArray = self.inTitles;
        [self.tableView reloadData];
    }

    else
    {
        self.currentArray = self.outTitles;
        [self.tableView reloadData];
    }

    [self.tableView reloadData];
    //self.sideMenu.openStatusBarStyle = UIStatusBarStyleLightContent;
    //self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}


#pragma mark -
#pragma mark UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UINavigationController *navigationController = (UINavigationController *)self.sideMenuViewController.contentViewController;

    switch (indexPath.row) {
        case 0:

            if([PFUser currentUser])
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Already Logged In" message:@"Proceeding to the login screen will log you out of your current session." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

                alert.tag = 2;
                [alert show];
            }
            /*
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"login"]];
            [self.sideMenuViewController hideMenuViewController];
             */
            break;
        case 1:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"register"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"about"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 3:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"who"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 4:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"request"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 5:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"my"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 6:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"submit"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 7:
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"debates"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 8:
            [PFUser logOut];
            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"login"]];
            [self.sideMenuViewController hideMenuViewController];
            break;
        default:
            break;
    }
}

#pragma mark -
#pragma mark UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return [self.currentArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }

    /*
    NSArray *titles = @[@"Login", @"Register", @"About Us", @"Who's Who", @"Request a Position", @"My Debates", @"Submit an Idea", @"Debates", @"Logout"];
    NSArray *images = @[@"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png", @"group-50white@2x.png"];
     */
    cell.textLabel.text = self.currentArray[indexPath.row];
    //cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];

    return cell;
}

#pragma mark -
#pragma mark RESideMenu Delegate

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willShowMenuViewController");
}

- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didShowMenuViewController");
}

- (void)sideMenu:(RESideMenu *)sideMenu willHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willHideMenuViewController");
}

- (void)sideMenu:(RESideMenu *)sideMenu didHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didHideMenuViewController");
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if(alertView.tag == 2)
    {
        if(buttonIndex == 1)
        {
            [PFUser logOut];
            [self.tableView reloadData];
            UINavigationController *navigationController = (UINavigationController *)self.sideMenuViewController.contentViewController;

            navigationController.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"login"]];
            [self.sideMenuViewController hideMenuViewController];

        }
    }


}

@end

为了在我的 tableview 菜单中实现/使用两个数组,我应该做一些不同的事情吗?

谢谢!

4

1 回答 1

1

我相信我刚刚解决了我自己的问题哈哈。我正在尝试将更改数组的代码放在不同的方法中,并且这个方法有效:

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willShowMenuViewController");

    self.inTitles = [NSArray arrayWithObjects:@"Login", @"Register", @"About Us", @"Who's Who", @"Request a Position", @"My Debates", @"Submit an Idea", @"Debates", @"Logout", nil];

    self.outTitles = [NSArray arrayWithObjects:@"Login", @"Register", @"About Us", @"Who's Who", nil];

    if([PFUser currentUser])
    {
        self.currentArray = self.inTitles;
        [self.tableView reloadData];
    }

    else
    {
        self.currentArray = self.outTitles;
        [self.tableView reloadData];
    }

    [self.tableView reloadData];
}

每次出现菜单时都会调用它。

于 2013-11-03T14:11:05.627 回答