ios - didSelectRowAtIndexPath 在带有侧边菜单的 ios6 中不起作用

标签 ios uitableview ios6 didselectrowatindexpath

我正在使用包含侧边菜单的应用程序。

当我在侧边菜单中选择显示 UItableview 的选项时,此表位于 FirstViewController 中,当我在 firstViewController 中选择特定行时,它不会导航到 SecondView Controller。

对于侧面菜单,我正在从 GitHUb 中获取现成的模板,其中包含

1)JWSlideMenuController
2)JWNavigationController
3)JWSlideMenuViewController

我在这里附加了 JWNavigationCOntroller.h 和 JWNavigationCOntroller.m 文件的代码 还有 FirstViewController.h 和 FirstViewController.m 文件

 **JWNavigationController.h**





//  JWNavigationController.h
    //  JWSlideMenu
    //
    //  Created by Jeremie Weldin on 11/22/11.
    //  Copyright (c) 2011 Jeremie Weldin. All rights reserved.
    //

    #import <UIKit/UIKit.h>
    @class JWSlideMenuController;

    @interface JWNavigationController : UIViewController <UINavigationBarDelegate>

    @property (nonatomic, retain) UINavigationBar *navigationBar;
    @property (nonatomic, retain) UIView *contentView;
    @property (nonatomic, retain) JWSlideMenuController *slideMenuController;
    @property (nonatomic, retain, readonly) UIViewController *rootViewController;

    - (id)initWithRootViewController:(UIViewController *)rootViewController;
    - (void)pushViewController:(UIViewController *)controller;

    - (UIViewController *)popViewController;

    @end

JWNavigationController.m

//  JWNavigationController.m
//  JWSlideMenu
//
//  Created by Jeremie Weldin on 11/22/11.
//  Copyright (c) 2011 Jeremie Weldin. All rights reserved.
//

#import "JWNavigationController.h"
#import "JWSlideMenuViewController.h"

@interface JWNavigationController(Private)

-(UIViewController*)removeTopViewController;

@end

@implementation JWNavigationController

@synthesize navigationBar;
@synthesize contentView;
@synthesize slideMenuController;
@synthesize rootViewController=_rootViewController;


#pragma mark - View lifecycle

- (id)init
{
    self = [super init];
    if (self) {

        CGRect masterRect = [[UIScreen mainScreen] bounds];
        CGRect contentFrame = CGRectMake(0.0, 44.0, masterRect.size.width, masterRect.size.height - 44.0);
        CGRect navBarFrame = CGRectMake(0.0, 0.0, masterRect.size.width, 44.0);

        self.view = [[[UIView alloc] initWithFrame:masterRect] autorelease];
        self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.view.backgroundColor = [UIColor whiteColor];

        self.contentView = [[[UIView alloc] initWithFrame:contentFrame] autorelease];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:self.contentView];

        self.navigationBar = [[[UINavigationBar alloc] initWithFrame:navBarFrame] autorelease];

        self.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.navigationBar.delegate = self;
        [self.view insertSubview:self.navigationBar aboveSubview:self.contentView];
        self.navigationBar.backgroundColor=[UIColor whiteColor];



    }
    return self;
}


- (id)initWithRootViewController:(JWSlideMenuViewController *)rootViewController
{
    self = [self init];
    if(self) {
        _rootViewController = rootViewController;
        UIBarButtonItem *menuButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon_20x20.png"] style:UIBarButtonItemStyleBordered target:self.slideMenuController action:@selector(toggleMenu)] autorelease];
        rootViewController.navigationItem.leftBarButtonItem = menuButton;
        [self addChildViewController:rootViewController];
        [self.contentView addSubview:rootViewController.view];
        [self.navigationBar pushNavigationItem:rootViewController.navigationItem animated:YES];
        //rootViewController.navigationController = self;
    }
    return self;
}

#pragma mark - UINavigationBarDelegate


- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
    UIViewController *controller = [self.childViewControllers lastObject];

    if (item==controller.navigationItem) //Will now called only if a back button pop happens, not in manual pops
    {
        [self removeTopViewController];
    }
}

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item
{

}

#pragma mark - Stack Interaction

- (void)pushViewController:(JWSlideMenuViewController *)controller
{
    [self addChildViewController:controller];
    [self.navigationBar pushNavigationItem:controller.navigationItem animated:YES];
    controller.navigationController = self;

    controller.view.frame = self.contentView.bounds;

    if([self.childViewControllers count] == 1)
    {
        [self.contentView addSubview:controller.view];
    }
    else
    {
        UIViewController *previousController = [self.childViewControllers   objectAtIndex:[self.childViewControllers count]-2];
        [self transitionFromViewController:previousController toViewController:controller duration:0.5 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
     }
}


- (UIViewController *)popViewController
{
    //Can use this to pop manually rather than back button alone
    UIViewController *controller = [self.childViewControllers lastObject];
    UIViewController *previousController = nil;
    if([self.childViewControllers count] > 1)
    {
        previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2];
        previousController.view.frame = self.contentView.bounds;
    }

    [self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
    [controller removeFromParentViewController];

    if(self.navigationBar.items.count > self.childViewControllers.count)
        [self.navigationBar popNavigationItemAnimated:YES];

    return controller;
}

- (void)viewDidUnload
{
    _rootViewController = nil;
    self.navigationBar = nil;
    self.contentView = nil;

    self.slideMenuController = nil;

    [super viewDidUnload];
}

- (void)dealloc {
    [_rootViewController release];
    [navigationBar release];
    [contentView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(UIViewController*)removeTopViewController
{
    UIViewController *controller = [self.childViewControllers lastObject];

    UIViewController *previousController = nil;
    if([self.childViewControllers count] > 1)
    {
        previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2];
        previousController.view.frame = self.contentView.bounds;


    }


    [self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
    [controller removeFromParentViewController];

    return controller;
}

@end

这里是代码FirstViewController.h

    //  FirstViewController.h

//  Created by mobile on 12/18/13.
//  Copyright (c) 2013 Jeremie Weldin. All rights reserved.
//

#import "JWSlideMenuViewController.h"

@interface FirstViewController : JWSlideMenuViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (strong,nonatomic) NSMutableArray *array;

@property (retain, nonatomic) IBOutlet UITableView *myTableView;
@end

  Here is code for FirstViewController.m
 //  FirstViewController.m
//  
//
//  Created by mobile on 12/18/13.
//  Copyright (c) 2013 Jeremie Weldin. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "JWNavigationController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize array,myTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    array=[[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten" ,nil];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array 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.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];

    return cell;
}

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





    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
            UIViewController *controller = [[[SecondViewContRoller  alloc] init] autorelease];
             [self.navigationController pushViewController:controller];
    }

- (void)viewDidUnload
{
    [self setMyTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
   [super dealloc];  }   @end

   **I am also Try This Code but not working**
        SecondViewController *second=[[SecondViewContRoller alloc]initWithNibName:@" SecondViewContRoller" bundle:nil];
                [self.navigationController pushViewController:controller];

JWSlideMenuController.h 包含以下代码


//  JWSlideMenuController.h
//  JWSlideMenu
//
//  Created by Jeremie Weldin on 11/14/11.
//  Copyright (c) 2011 Jeremie Weldin. All rights reserved.
//

#import <UIKit/UIKit.h>
@class JWNavigationController;
@class JWSlideMenuViewController;

@interface JWSlideMenuController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (retain, nonatomic) UITableView *menuTableView;
@property (retain, nonatomic) UIView *menuView;
@property (retain, nonatomic) UIToolbar *contentToolbar;
@property (retain, nonatomic) UIView *contentView;
@property (retain, nonatomic) UIColor *menuLabelColor;

-(IBAction)toggleMenu;
-(JWNavigationController *)addViewController:(JWSlideMenuViewController *)controller withTitle:(NSString *)title andImage:(UIImage *)image;


@end

JWSlideMenuController.m 包含以下用于 didSelectRowAtIndexPath 的代码


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

    if([contentView.subviews count] == 1){
        [[contentView.subviews objectAtIndex:0] removeFromSuperview];
    }


    UIViewController* controller = (UIViewController*)[self.childViewControllers objectAtIndex:indexPath.row];
    controller.view.frame = self.contentView.bounds;
    [contentView addSubview:controller.view];
    [self toggleMenu];





}

最佳答案

试试这个。

从 github 下载 zib 以获取 MFSideMenu 类,并将此代码用于 TableView 的 did select 方法。它在 ios6、ios7 中也能正常工作....

yourViewController *dealsVC = [[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil];
        //[self.navigationController pushViewController:dealsVC animated:YES];


        UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
        NSArray *controllers = [NSArray arrayWithObject:dealsVC];
        navigationController.viewControllers = controllers;
        [self.menuContainerViewController setMenuState:MFSideMenuStateClosed];

关于ios - didSelectRowAtIndexPath 在带有侧边菜单的 ios6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703832/

相关文章:

iphone - 使用 NSDictionary 'initWithContentsOfURL' 代替 Reachability.h

ios - 自定义 UitableViewCell 显示表格某些行的重复数据

cocoa-touch - 如何制作可扩展的表格 View 单元格?

ios - 如何从 TableView 中添加/删除 subview

ios - iOS 6 上对 safari mobile 的 CSS 样式滚动条支持消失了吗?

iphone - 当键盘未进行更改时检测 UITextField 内容的更改

ios - pushViewController 做一个黑色 View

iphone - 连续切换 Sprite 图像(不显示动画)

ios - UIImageView调整大小消除空白IOS

objective-c - 如何制作自定义 tableView 单元配件