简体   繁体   English

在 Swift 中单击表视图单元格时如何显示新的视图控制器

[英]How to show new View Controller when a Table View Cell is clicked in Swift

I am trying to show a new view controller when a table view cell is clicked.当单击表视图单元格时,我试图显示一个新的视图控制器。 But my problem is when I click table view cell the new view controller does not appear.但我的问题是当我单击表格视图单元格时,新的视图控制器没有出现。 I already have looked at solutions on SO but it didn't work.我已经看过关于 SO 的解决方案,但它没有用。

Here is a screenshot of storyboard这是故事板的屏幕截图在此处输入图片说明

And here is the relevant code:这是相关的代码:

import UIKit

class RestaurantTableViewController: UITableViewController {

    let cellIdentifier: String = "restaurantCell"


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "restaurantSegue" {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let destination = storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController
            navigationController?.pushViewController(destination, animated: true)
        }
    }
}

extension RestaurantTableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "restaurantSegue", sender: self)
    }

}

Note: i also followed Apple's documentation in the link Apple's guide .注意:我还在链接Apple's guide 中遵循了 Apple 的文档。 But this didn't work as well但这并没有奏效

According to the screenshot the segue is connected to the table view cell.根据屏幕截图,segue 连接到表格视图单元格。

If so delete didSelectRowAt because the segue is performed directly如果是这样删除didSelectRowAt因为segue是直接执行的

 
 
  
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "restaurantSegue", sender: self) }
 
 

The second issue is if you are using a segue you must not call instantiateViewController .第二个问题是,如果您使用的是 segue,则不得调用instantiateViewController

If you don't have to pass data to the next view controller you can even delete如果您不必将数据传递给下一个视图控制器,您甚至可以删除

 
 
  
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "restaurantSegue" { let storyboard = UIStoryboard(name: "Main", bundle: nil) let destination = storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController navigationController?.pushViewController(destination, animated: true) } }
 
 

Otherwise get the destination controller from the segue in prepare(for and use that.否则从prepare(for的 segue 获取destination控制器prepare(for并使用它。

If the segue is still not performed then there's something wrong with the identifier.如果仍然没有执行转场,则标识符有问题。

You just need to make the segue.destination as your viewcontroller, no need to push it.您只需要将segue.destination作为您的segue.destination ,无需推送它。 The performSegue method will take care of it. performSegue 方法会处理它。 Like this:像这样:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "restaurantSegue" {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            segue.destination =  storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController

        }
    }

暂无
暂无

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

相关问题 在Swift中单击Table View的单元格时如何调用(显示)另一个View Controller - How to call (show) another View Controller when clicked on the cell of Table View in Swift 通过单击表视图中的单元格 - Swift iOS打开新的视图控制器 - Open new View Controller by clicking Cell in Table View - Swift iOS 在表视图单元格中单击按钮时如何使用参数调用视图控制器 - How to call view controller with parameters when button clicked in table view cell 在快速的iOS 8中,当单击一个单元格时,如何获取它以打开另一个视图控制器? - In swift iOS 8 when a cell is clicked, how do I get it to open another view controller? 当数据源和委托与View Controller分开时,如何将Table View的单击项发送到其他View Controller - How to send clicked item of Table View to other view controller when datasource and delegate are separate from View Controller 如何向右滑动以在Swift 3中显示新的View Controller? - How to swipe right to show new View Controller in Swift 3? 单击表格视图单元格时加载缓慢 - slow loading when clicked on table view cell Swift 4-如何从当前视图控制器设置和显示新的根视图控制器 - Swift 4 - How to set and show a new root view controller from current view controller 按下indexPath单元格时如何导航或显示其他视图控制器 - how to navigate or show other view controller when indexPath cell pressed Swift CollectionView 单元格中的 CollectionView 单元格 - 当前新视图 Controller - Swift CollectionView Cell in a CollectionView Cell - Present New View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM