我可以在一个UITableViewDelegate中使用多个segue吗? [英] Can I use multiple segues with one UITableViewDelegate?

查看:116
本文介绍了我可以在一个UITableViewDelegate中使用多个segue吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列出多种类型对象的 UITableView ,我想根据用户选择的对象类型来转换到不同的视图。

I have a UITableView listing multiple types of objects, and I'd like to segue to a different view depending on which type of object the user selects.

是否可以通过使用多个segue来实现,如果是这样,怎么做?

Is it possible to do this by using multiple segues, and, if so, how?

推荐答案

当然!通过从tableViewController (不是一行,tableViewController本身)中的拖动到下一个视图,在故事板上定义所有segue。给他们ID,这样你就知道要拨打哪一个。当您可视化定义所有segue时,请转到代码。在tableView的委托中,在 didSelectRowAtIndexPath 中,只需通过选中 indexPath.row 来调用所需的segue:

Of course ! Define all your segues on your storyboard, by ctrl-dragging from the tableViewController (not a row, the tableViewController itself) to the next view. Give them IDs, so you know which one to call. When all your segues are defined visually, go to the code. In your tableView's delegate, in didSelectRowAtIndexPath, simply call the segue you want, by checking indexPath.row :

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

    switch (indexPath.row) {
        case 0: [self performSegueWithIdentifier:@"Segue0" sender:self];
                break;
        case 1: [self performSegueWithIdentifier:@"Segue1" sender:self];
                break;
        [...]
        default: break;
    }
}

这样,带有Segue0ID的segue将在用户选择第一行时触发,依此类推。

That way, the segue with the ID "Segue0" will be fired when the user selects the first row, and so on.

您还可以添加以下行: [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow :0 inSection:1] animated:YES]; didSelectRowAtIndexPath 的开头,所以在用户触摸它后行不会保持选中状态!

You can also add the line : [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:YES]; at the beginning of didSelectRowAtIndexPath so the row does not remain selected after the user touched it !

编辑:这适用于静态和动态单元格!小心ctrl-拖动你的segue从tableViewController,而不是一个单元格!

Edit : This works for both static and dynamic cells ! Be careful to ctrl-drag your segue from the tableViewController, not a cell !

这篇关于我可以在一个UITableViewDelegate中使用多个segue吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆