【发布时间】:2019-04-20 01:04:30
【问题描述】:
我希望在选择表格单元格时打开导航控制器。导航控制器必须具有所选单元格中包含的文本的标题。
我认为我的代码非常接近,但似乎无法弄清楚我做错了什么。
当我从表格中选择一个单元格时,什么都不会打开。
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)
}
但我需要视图控制器将自己呈现为 Show Detail 故事板样式,其中 detailController 覆盖 FirstViewController。
你不能回去的地方。
【问题讨论】:
-
我认为这可能是重复的。 stackoverflow.com/questions/9670797/…
标签: ios swift uitableview