ios - UINavigationController 没有后退按钮

标签 ios uitableview swift3 uinavigationcontroller

非常简单的场景,有很多关于此的帖子,但我仍然卡住了。我有一个嵌入在 UINavigationController 中的 UITableView 最后一个 UIViewController 应该在 UITableViewCell被选中。当我连接所有这些时,行选择没有任何反应。

我可以通过使用 didSelectRowAtIndexPath 并通过其 id 引用 segue 来获得要呈现的详细 View 。但是,执行此操作时没有后退按钮。

enter image description here

enter image description here 类 MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!
    var configJson: JSON = []
    var config: [Tab] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        parseConfig()
    }

    func parseConfig() {
        let configParser = ConfigParser(configJson: configJson)
        config = configParser.parse()
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.config.count
    }

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

        let tab = self.config[indexPath.row]

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

        cell.textLabel?.text = tab.title

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "contentSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if (segue.identifier == "contentSegue") {
            let nextVC = segue.destination as? ContentViewController
            let indexPath = self.tableview.indexPathForSelectedRow
            let tab = self.config[(indexPath?.row)!]
            nextVC?.tab = tab
        }
    }
}

一些其他注意事项: 1. 我的单元格标识符在 IB 中设置为“单元格”
2. 我的 segue 在 IB 中设置为“显示”,标识符为“contentSegue”
3. 转场是从原型(prototype)单元格到内容 View Controller 。

我完全不知所措。谁能帮忙?谢谢。

最佳答案

您确定已将其设置为选择转场,而不是辅助操作吗?您可以通过按住 ctrl 单击 Storyboard中的单元格来检查这一点 - 确保您触发的 segue 是选择。

还有……

当您在 Storyboard中添加 segue 时,无需在 didSelectRowAt 中触发它 - 因此您可以删除该函数。

并且,要消除对字符串比较的依赖(并消除强制解包可选),试试这个……

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nextVC = segue.destination as? ContentViewController, 
       let cell = sender? as UITableViewCell,
       let indexPath = tableview.indexPath(for: cell) {

        let tab = self.config[indexPath.row]
        nextVC.tab = tab
    }
}

关于ios - UINavigationController 没有后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46040245/

相关文章:

iphone - 在一个类中管理两个 TableView

ios - 更改 UINavigation Controller 以与 SlidePanel 配合使用

ios - UIGraphicsGetImageFromCurrentImageContext() 在 UITableViewCell 中返回空白图像

swift - UISearchBar 和 Firebase 数据库

swift - 如何检查字符是否为单词边界

javascript - 解析iOS SDK + 云代码: How to update user

ios - 导入我的框架后需要将 UIKit 显式导入项目中的所有 UIKit 子类

ios - index ScrollTo 不适用于 TableViewCell Swift 中的最后一个 CollectionView

iOS swift : Pushing a View Onto the Stack From Within a Custom Tableview Cell

ios - 命令因信号 : Segmentation fault: 11 due to declaration 而失败