【问题标题】:How to write if/else statement in swift so that when a specific cell is clicked in iOS tableview it goes to a specific viewController如何在 swift 中编写 if/else 语句,以便在 iOS 表格视图中单击特定单元格时,它会转到特定的 viewController
【发布时间】: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 我建议对您的帖子进行编辑。它只是稍微调整格式。它应该可以帮助您发现问题。

标签: ios swift


【解决方案1】:

您的第二个 if 语句永远不会触发,因为您是从第一个 if 语句内部调用它。单元格行不能同时为 0 和 20。你需要一个 else if 语句。

if condition {
   do something
} else if condition2 {
   do something else
} else {
   do something else
}

【讨论】:

  • 哇,我现在明白了,谢谢大家。
【解决方案2】:
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if(indexPath.section == 0)
            {
                newworkrequest()
            }else if(indexPath.section == 1)
            {
                requestlist()
            }
}

@objc func newworkrequest()
{
    let viewcontroller : new_work_request = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "newworkRequest") as! new_work_request
    self.navigationController?.pushViewController(viewcontroller, animated: true)
    }
    @objc func requestlist()
    {
        let viewcontroller : workrequestlist = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "requestlist") as! workrequestlist
        self.navigationController?.pushViewController(viewcontroller, animated: true)
    }

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2018-12-23
    相关资源
    最近更新 更多