ios - 如何打开带有所选单元格标题的导航 Controller

标签 ios swift uitableview

我希望在选择表格单元格时打开导航 Controller 。导航 Controller 必须具有所选单元格中包含的文本的标题。

我认为我的代码非常接近,但似乎无法弄清楚我做错了什么。

当我从表格中选择一个单元格时,没有打开任何东西。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        myCell.textLabel?.font = .boldSystemFont(ofSize: 18)
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let item: StockModel = feedItems[indexPath.row] as! StockModel
        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")

        print(titleStr)

    }

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "customerDetails" {

        let destinationVC = segue.destination as UIViewController
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        destinationVC.navigationItem.title = myCell.textLabel!.text
        }
    }

}

新更新:

以下现在有效:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    let cellIdentifier: String = "stockCell"
    let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
    myCell.textLabel?.textAlignment = .center

    // Get the stock to be shown
    let item: StockModel = feedItems[indexPath.row] as! StockModel
    // Configure our cell title made up of name and price

    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")

    print(titleStr)
    // Get references to labels of cell
    myCell.textLabel!.text = titleStr

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "detailController")
    controller.title = titleStr
    navigationController?.pushViewController(controller, animated: true)

}

但我需要 View Controller 将自身呈现为 Show Detail Storyboard样式,其中 detailController 覆盖 FirstViewController。

回不去的地方。

最佳答案

检查 segue 是否连接到 main.storyboard 文件中。如果是,请检查标识符是否与代码中的标识符匹配。如果这不起作用,可能是因为您正在 prepareForSegue 中创建一个新单元格,并且要解决这个问题,您必须为每个单元格创建一个全局变量。如果所有这些都不起作用,请告诉我。你能附上 main.storyboard 文件吗?

关于ios - 如何打开带有所选单元格标题的导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55757086/

相关文章:

ios - 将非 App Store iOS 应用程序转移到另一个 iOS 企业开发者帐户

没有参数的 Swift 函数

swift - 如何从命令行编译 Swift

iphone - 检测 UITableView SECTION 上的触摸?

ios - 隐藏某个表格行 - Swift 4

ios - 当键盘出现时向上移动文本字段会留下黑条

ios - 由于 "bundle format unrecognized, invalid, or unsuitable",Xcode 构建失败

ios - 如何从 UIViewController 中删除传递的闭包

ios - 是否可以在不使用 UIBezierPath 的情况下绘制虚线

ios - 添加额外控件时的 UITableViewController 或 UIViewController