非常简单的场景,有很多关于此的帖子,但我仍然卡住了。我有一个嵌入在 UINavigationController
中的 UITableView
最后一个 UIViewController
应该在 UITableViewCell时显示
被选中。当我连接所有这些时,行选择没有任何反应。
我可以通过使用 didSelectRowAtIndexPath
并通过其 id 引用 segue 来获得要呈现的详细 View 。但是,执行此操作时没有后退按钮。
类 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/