如何根据 uitableview 行选择更改导航栏标题?
how to change navigation bar title according to uitableview row selection?
我在我的视图控制器上添加了一个导航栏。但是当我 运行 我的代码时,这一行: let cell = sender as! UITableViewCell
有问题。知道如何根据我的选择添加或更改导航栏标题吗?我的 UITableView
是一个数组列表。
虽然构建成功,但它一直显示
Thread 1: signal SIGABRT
除此之外,如何根据我在 UITableView
中的选择更改导航栏标题?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is DetailVC {
let destination = segue.destination as! DetailVC
let cell = sender as! UITableViewCell
destination.navigationItem.title = cell.textLabel?.text
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
}
查看控制器代码
import UIKit
class Developers {
var developerName: String?
var developerEmail: String?
var developerBalance: Float?
var developerHP: String?
init(dvName: String, dvHP: String) {
self.developerName = dvName
self.developerHP = dvHP
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
var developerArray = [Developers]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//navigationItem.searchController = UISearchController(searchResultsController: nil)
let developer = Developers(dvName: "Developer 0", dvHP: "Phone Number: #0")
developerArray.append(developer)
let developer1 = Developers(dvName: "Developer 1", dvHP: "Phone Number: #1")
developerArray.append(developer1)
let developer2 = Developers(dvName: "Developer 2", dvHP: "Phone Number: #2")
developerArray.append(developer2)
let developer3 = Developers(dvName: "Developer 3", dvHP: "Phone Number: #3")
developerArray.append(developer3)
let developer4 = Developers(dvName: "Developer 4", dvHP: "Phone Number: #4")
developerArray.append(developer4)
let developer5 = Developers(dvName: "Developer 5", dvHP: "Phone Number: #5")
developerArray.append(developer5)
let developer6 = Developers(dvName: "Developer 6", dvHP: "Phone Number: #6")
developerArray.append(developer6)
myTableView.dataSource = self
myTableView.delegate = self
}
//MARK:- UITableView methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return developerArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "MyCell")
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "MyCell")
}
cell?.textLabel?.text = developerArray[indexPath.row].developerName
cell?.detailTextLabel?.text = developerArray[indexPath.row].developerHP
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailVC = DetailVC()
detailVC.titleStringViaSegue = self.developerArray[indexPath.row].developerName
self.navigationController?.pushViewController(detailVC, animated: true)
performSegue(withIdentifier: "DevToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailVC {
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
}
}
在目标控制器上创建 navigationTitle 属性。
class Destination: UIViewController {
var navigationTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = navigationTitle
}
}
在推送目标控制器时检查标题是否可用
if segue.destination is DetailVC {
let destination = segue.destination as! DetailVC
if let cell = sender as? UITableViewCell , let title = cell.textLabel?.text {
destination.navigationTitle = title
}
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
这是您的 DetailVC
class DetailVC: UIViewController {
@IBOutlet weak var productLabel: UILabel!
var titleStringViaSegue: String!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.title = self.titleStringViaSegue
}
}
像这样更改您的 didselect
方法。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destination = storyboard.instantiateViewController(identifier: "DetailVC") as! DetailVC
destination.titleStringViaSegue = developerArray[indexPath.row].name
self.navigationController?.pushViewController(destination, animated: true)
}
您可以像这样提供故事板 ID。
使用tableView
的didSelect
功能:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destination = storyboard.instantiateViewController(identifier: "<StoryboardID>") as! Destination //StoryboardID of Destination
destination.navigationTitle = developerArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
我在我的视图控制器上添加了一个导航栏。但是当我 运行 我的代码时,这一行: let cell = sender as! UITableViewCell
有问题。知道如何根据我的选择添加或更改导航栏标题吗?我的 UITableView
是一个数组列表。
虽然构建成功,但它一直显示
Thread 1: signal SIGABRT
除此之外,如何根据我在 UITableView
中的选择更改导航栏标题?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is DetailVC {
let destination = segue.destination as! DetailVC
let cell = sender as! UITableViewCell
destination.navigationItem.title = cell.textLabel?.text
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
}
查看控制器代码
import UIKit
class Developers {
var developerName: String?
var developerEmail: String?
var developerBalance: Float?
var developerHP: String?
init(dvName: String, dvHP: String) {
self.developerName = dvName
self.developerHP = dvHP
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
var developerArray = [Developers]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//navigationItem.searchController = UISearchController(searchResultsController: nil)
let developer = Developers(dvName: "Developer 0", dvHP: "Phone Number: #0")
developerArray.append(developer)
let developer1 = Developers(dvName: "Developer 1", dvHP: "Phone Number: #1")
developerArray.append(developer1)
let developer2 = Developers(dvName: "Developer 2", dvHP: "Phone Number: #2")
developerArray.append(developer2)
let developer3 = Developers(dvName: "Developer 3", dvHP: "Phone Number: #3")
developerArray.append(developer3)
let developer4 = Developers(dvName: "Developer 4", dvHP: "Phone Number: #4")
developerArray.append(developer4)
let developer5 = Developers(dvName: "Developer 5", dvHP: "Phone Number: #5")
developerArray.append(developer5)
let developer6 = Developers(dvName: "Developer 6", dvHP: "Phone Number: #6")
developerArray.append(developer6)
myTableView.dataSource = self
myTableView.delegate = self
}
//MARK:- UITableView methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return developerArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "MyCell")
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "MyCell")
}
cell?.textLabel?.text = developerArray[indexPath.row].developerName
cell?.detailTextLabel?.text = developerArray[indexPath.row].developerHP
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailVC = DetailVC()
detailVC.titleStringViaSegue = self.developerArray[indexPath.row].developerName
self.navigationController?.pushViewController(detailVC, animated: true)
performSegue(withIdentifier: "DevToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailVC {
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
}
}
在目标控制器上创建 navigationTitle 属性。
class Destination: UIViewController {
var navigationTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = navigationTitle
}
}
在推送目标控制器时检查标题是否可用
if segue.destination is DetailVC {
let destination = segue.destination as! DetailVC
if let cell = sender as? UITableViewCell , let title = cell.textLabel?.text {
destination.navigationTitle = title
}
destination.developer = developerArray[(myTableView.indexPathForSelectedRow?.row)!]
myTableView.deselectRow(at: myTableView.indexPathForSelectedRow!, animated: true)
}
这是您的 DetailVC
class DetailVC: UIViewController {
@IBOutlet weak var productLabel: UILabel!
var titleStringViaSegue: String!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.title = self.titleStringViaSegue
}
}
像这样更改您的 didselect
方法。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destination = storyboard.instantiateViewController(identifier: "DetailVC") as! DetailVC
destination.titleStringViaSegue = developerArray[indexPath.row].name
self.navigationController?.pushViewController(destination, animated: true)
}
您可以像这样提供故事板 ID。
使用tableView
的didSelect
功能:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destination = storyboard.instantiateViewController(identifier: "<StoryboardID>") as! Destination //StoryboardID of Destination
destination.navigationTitle = developerArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}