[英]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.