【发布时间】:2019-04-26 14:47:07
【问题描述】:
我有一个带有多个单元格的 tableView,这些单元格转到一个 viewController。 I'm setting up a new viewController for a specific cell that, when selected, will open its own viewController for that specific cell. How do I add another if/else statements for a second cell when selected to go to its own ViewController which will be the third VC?如何在 Swift 中正确实现这一点?
I already have one if/else statement that when the cell in section"0" row "0" is selected to go to the reciprocity view controller.它工作正常,但是,如何在“0”行“20”部分为单元格添加另一个语句以转到其自己的 viewController。我是 swift 的新手,并试图正确学习。任何帮助将不胜感激。
`override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if ((indexPath.section == 0) && (indexPath.row == 0)) {
performSegue(withIdentifier: "reciprocityView", sender: nil);
if ((indexPath.section == 0) && (indexPath.row == 20)) {
performSegue(withIdentifier: "InterestViewController", sender: nil)
}
} else {
let productLine = productLines[indexPath.section]
let product = productLine.products[indexPath.row]
selectedProduct = product
performSegue(withIdentifier: "showProductDetail", sender: nil)}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showProductDetail" {
let DetailVC = segue.destination as! DetailViewController
DetailVC.product = selectedProduct
}
}
}`
由于我添加了 ((indexPath.section == 0) && (indexPath.row == 20,因此该单元格继续转到默认的 viewController 而不是 InterestViewController。
【问题讨论】:
-
修正你的缩进,你会看到你的问题;你第二个
if在你的第一个if里面。第二个if应该是else if -
你能给我举个例子吗?我是自学成才,仍在快速学习。抱歉,我正在学习如何使用 if/else 语句。给我看一个例子,我可以学习正确的语法。你也可以通过修复缩进来告诉我你的意思。我认为这是正确的,但我一定是错的
-
@blaw 我建议对您的帖子进行编辑。它只是稍微调整格式。它应该可以帮助您发现问题。