简体   繁体   English

Swift Table View Controller Segue无法自动运行

[英]Swift Table View Controller Segue Not Working Automatically

Steps taken: 采取的步骤:

  1. Created new single view project 创建了新的单视图项目
  2. Deleted default view controller 删除默认视图控制器
  3. Added new Table View Controller 添加了新的表视图控制器
  4. Embedded TVC into a Navigation Controller 将TVC嵌入式到导航控制器中
  5. Added a child TVC 添加了子TVC
  6. Added custom class files and associated to each TVC 添加了自定义类文件并与每个TVC关联
  7. Created a Show segue from the first TVC to the child 创建了从第一个TVC到孩子的表演节目
  8. Implemented required methods, #of sections, # of rows, cellForRowAtIndexPath 已实现所需的方法,部分数,行数,cellForRowAtIndexPath

All of the tutorials I have watched and read online only include the steps I show above, and the segues start working fine. 我在网上观看和阅读的所有教程都只包含我上面显示的步骤,并且搜索开始运行良好。 However, in my case I can not get it to work until I add the following: 但是,就我而言,直到添加以下内容,我才能使它工作:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("detail", sender: tableView)
}

I'm totally fine with implementing the didSelectRowAtIndexPath method if it is required. 如果需要的话,我完全可以实现didSelectRowAtIndexPath方法。 I'm just wondering if I am missing something, because it seems to work automatically in everything I've seen online. 我只是想知道我是否缺少某些东西,因为它似乎可以在我在线上看到的所有内容中自动运行。

Thanks in advance! 提前致谢!

If you click on the segue (storyboard) and on the side menu you have to assign the text "detail" from 如果单击segue(故事板)和侧面菜单,则必须从

performSegueWithIdentifier("detail", sender: tableView)

on the Identifier field. 标识符字段上。

Like the image below 就像下面的图片

在此处输入图片说明

Edit: 编辑:

Try the code above, rembember to change NAME_OF_THE_DETAIL_VIEW_CONTROLLER to your Detail ViewController. 尝试上面的代码,记住将NAME_OF_THE_DETAIL_VIEW_CONTROLLER更改为您的Detail ViewController。

In this code we set i set " self " before the performSegueWithIdentifier . 在这段代码中,我们设置我在performSegueWithIdentifier之前设置了“ self ”。 And when the button is clicked you set the segue.destinationViewController to your new ViewController. 然后单击按钮时,将segue.destinationViewController设置为新的ViewController。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("detail", sender: indexPath);
}


override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "detail") {
        var detailView =  segue.destinationViewController as! NAME_OF_THE_DETAIL_VIEW_CONTROLLER 
    }
}

Thank you all for your contributions. 谢谢大家的贡献。 I figured out that the problem was the following was missing from the cellForRowAtIndexPath tableView function. 我发现问题出在cellForRowAtIndexPath tableView函数中。

let cell = self.tableView.dequeueReusableCellWithIdentifier("custom", forIndexPath: indexPath) as! CustomCell

I had this line commented out, mostly because I have no idea what it does. 我将此行注释掉了,主要是因为我不知道它的作用。 :) :)

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

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