【问题标题】:select a viewcontroller programatically at didiSelectRowAtIndexpath in ios在 ios 中的 didSelectRowAtIndexpath 以编程方式选择视图控制器
【发布时间】:2015-10-05 09:25:51
【问题描述】:

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

当我点击带有@"l1" 标识符的单元格时,我想加载带有sw_front 的主视图控制器,用于SWRevealViewController。我不能使用 controll+drag 因为在她身上我使用了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];


    }
}

【问题讨论】:

  • - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 使用此方法获取点击了哪个单元格
  • 你想用 SWRevealViewController 改变当前可见的 ViewController 和另一个 ViewController 还是你想 push segue
  • 你使用了故事板或 xib
  • 我使用故事板@Anbu.Karthik
  • 是的,我想用 swrevealviewcontroller 和 sw_front @Islam Q 进行更改

标签: ios uitableview segue swrevealviewcontroller


【解决方案1】:

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

这里 viewController1viewController2 等是个人 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];
}

【讨论】:

  • 谢谢大家,我会检查一下并向您发送反馈。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多