简体   繁体   English

来自动态UITableView的动态搜索似乎不可能

[英]Dynamic segues from a Dynamic UITableView don't seem to be possible

I am trying to accomplish something that seems simple enough: I have a UITableView with dynamic UITableViewCells. 我试图完成一些看起来很简单的事情:我有一个带有动态UITableViewCells的UITableView。 When each cell is selected, I want to perform a segue to a different view controller in my Storyboard. 选中每个单元格后,我要对情节提要中的另一个视图控制器执行segue。 However, I want to create the segues programatically because I want to determine which ViewController gets called at runtime. 但是,我想以编程方式创建segues,因为我想确定在运行时调用哪个ViewController。 However, I can't seem to get it to work. 但是,我似乎无法正常工作。

Here's how I set up my code for the cell selection: 这是我为单元格选择设置代码的方式:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if(indexPath.row==0){

        var calculoViewController = CalculoViewController()
        var calcSegue = UIStoryboardSegue(identifier:"Menu_Calc", source: self, destination: calculoViewController)

        self.performSegueWithIdentifier("Menu_Calc", sender: self)
    }
    else
    {
        var deducVC = DeductionsViewController()
        var deducSegue = UIStoryboardSegue(identifier: "Menu_Deduc", source: self, destination: deducVC)
        self.performSegueWithIdentifier("Menu_Deduc", sender: self)
    }
}

However, this fails with: NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'Menu_Calc'' 但是,此操作失败:NSInvalidArgumentException',原因:“ Receiver()没有标识为“ Menu_Calc”的segue

Note that I don't have any drawn segues in my Storyboard, that is why I am creating them dynamically. 请注意,我的情节提要中没有绘制的序列,这就是为什么要动态创建它们的原因。 Previously I tried drawing them by connecting the prototype cell with the ViewControllers, but then every cell called the same segue, despite the code. 以前,我尝试通过将原型单元与ViewControllers连接来绘制它们,但是尽管有代码,但每个单元都调用相同的segue。

The correct way to perform the segue you just created would be this 执行刚刚创建的segue的正确方法是

var calcSegue = UIStoryboardSegue(identifier:"Menu_Calc", source: self, destination: calculoViewController)
calcSegue.perform()

If you want to create segues using the storyboard but not called on row selection, you can create a segue between the 2 viewcontrollers (not the prototype cell and VC) and give them an identifier. 如果要使用情节提要创建序列,但不调用行选择,则可以在2个视图控制器(不是原型单元和VC)之间创建序列,并为其指定一个标识符。 Then in your didSelectRow method, perform the segue with the correct identifier. 然后在您的didSelectRow方法中,使用正确的标识符执行segue。

Wow it is mid of 2018 and the issue is still lack of a valid answer! 哇,这是2018年年中,问题仍然缺少有效答案! I hope someone may provide a valid answer. 我希望有人可以提供有效答案。 I have more than two dynamic cells to more than two seguing-to view controllers. 我有两个以上的动态单元,也有两个以上的序列化视图控制器。

Yet, for Tutiplain's simple situation - only one if-else, I would suggest to use normal reusable cell's segue in else block. 但是,对于Tutiplain的简单情况-如果只有一种情况,我建议在else块中使用正常的可重用细胞的序列。 For first row in his case, use shouldPerformSegue(withIdentifier:sender:) to prevent it from this normally seguing; 对于他的情况下的第一行,请使用shouldPerformSegue(withIdentifier:sender :)防止它正常进行隔离; then use performSegue(withIdentifier: sender:) to manually do a special segue that is set up on storyboard by ctrl-drag from controller icon of seguing-from view controller to segueing-to view controller. 然后使用performSegue(withIdentifier:sender :)手动执行一个特殊的segue,由ctrl拖动在故事板上设置为从seguing的控制器图标-从视图控制器到segueing-到视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM