以编程方式在ios中的didiSelectRowAtIndexpath中选择一个viewcontroller

时间:2015-07-16 04:58:18

标签: ios uitableview segue swrevealviewcontroller

我有一个动态原型内容的tableview。我想在动态单元格点击时以编程方式调用特定的ViewController。我使用SWRevealViewController调用我的项目。

当我点击带有@"l1"标识符的第一个单元格时,我想加载sw_front的主视图控制器,在SWRevealViewController中使用。我不能使用controll + drag becaue在她使用tabviewcontrller。当我这样做时它会消失标签栏。

#import "ListViewController.h"

@interface ListViewController ()

@end

@implementation ListViewController
{
    NSArray *menuItems;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    menuItems = [NSArray arrayWithObjects:@"l1",@"l2",@"l3",@"l6",@"l4",@"l5",@"l7", nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [menuItems count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.reuseIdentifier isEqualToString:@""]) {

        UIViewController *mainviewcontroller = [[UIViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:mainviewcontroller];
        [self.navigationController presentViewController:navi animated:YES completion:nil];


    }
}

1 个答案:

答案 0 :(得分:0)

如果您已设置storyboard ID,则可以使用以下代码以编程方式推送。

此处 viewController1 viewController2 等是个人UIViewController的故事板ID。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *pushViewController = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"viewController%ld",indexPath.row]];

    [self.navigationController pushViewController:pushViewController animated:YES];
}