如何在选择特定 UITableViewCell 时进行推送 segue



我正在开发我的第一个iOS应用程序和快速的新学生。

一切正常,但是我无法弄清楚如何从特定单元格切换到第三个视图控制器。 我有一个IOS UITableView,有三个部分,总共(44(个单元格。 点击时的所有单元格都指向一个标题为:showProductDetaiDetailVC,这很好。 我遇到的问题是,我只需要在UITableView的第 0 部分第 5 行中只有 (1( 个特定单元格才能转到我标题为second view controller的自己的ViewController,而不是正常的 showProductDetail VC。 有没有一种智能方法可以使tableView第 0 部分第 5 行中的特定单元格在选择时转到第二个视图控制器?

这是我当前正在工作的代码。 我将如何编码以进行更改?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductTableViewCell
    // Configure the cell...
    let productLine = productLines[indexPath.section]
    let products = productLine.products
    let product = products[indexPath.row]
    cell.product = product
    return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let productLine = productLines[section]
    return productLine.name
}
// Mark: UITableViewDelegate
var selectedProduct: Product?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let productLine = productLines[indexPath.section]
    let product = productLine.products[indexPath.row]
    selectedProduct = product
    performSegue(withIdentifier: "ShowProductDetail", sender: nil)

}
// Mark: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "ShowProductDetail" {
        let DetailVC = segue.destination as! DetailViewController
        DetailVC.product = selectedProduct
    }
}

看看我在这里做了什么? 您可以使用 indexPath 参数获取用户触摸的部分和行,然后您可以设置您的编程 segue。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if ((indexPath.section == 0) && (indexPath.row == 5)) {
        performSegue(withIdentifier: "GoToSecondViewController", sender: nil)
     } else {
        let productLine = productLines[indexPath.section]
        let product = productLine.products[indexPath.row]
        selectedProduct = product
        performSegue(withIdentifier: "ShowProductDetail", sender: nil)
    }
}

为所需的部分和行编写 segue 代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 && indexPath.row == 5 {
        //Perform Segue
    } else {
        //Rest of the functionality
    }
}

确保已连接正确的 Sugue 并在情节提要中提供了正确的标识符。

你不需要写prepare Segue 方法

最新更新