ios - 如何在 swift 中编写 if/else 语句,以便在 iOS 表格视图中单击特定单元格时,它会转到特定的 viewController
问题描述
我有一个带有多个单元格的 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 语句内部调用它。单元格行不能同时为 0 和 20。在此之外您需要一个 else if 语句。
if condition {
do something
} else if condition2 {
do something else
} else {
do something else
}
推荐阅读
- python - 有没有更简单的方法来打印给定变量而不必一遍又一遍地使用 print 函数?
- d3.js - d3 用函数改变轴上刻度的颜色
- google-ads-api - 我无法通过 Google 登录联系客户 AdWords 帐户
- java - 用不同的软件放气算法不同的结果
- reactjs - 在 react.jsx 中添加一个演示按钮,它将填充表单数据
- pg-promise - pg-promise 和行级安全性
- json - 将数据传递到前端时出现一些错误
- python - 用户输入错误后从 .txt 文件中删除多余的选项卡
- java - 如何给线性布局上下文
- java - 重新启动活动不会完全杀死旧活动?