我在故事板的单元格中有一个UITableViewController
标签。我想在下一个控制器的标题中显示所选单元格的标题。如何显示标题?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.destination is ViewController) {
//code to showing title of my cell
}
}
答案 0 :(得分:0)
您只需要在if语句中获取目标ViewController,如下所示:
//get destination view an set the title
let vc=segue.destination
//set title
vc.title="My Title"
答案 1 :(得分:0)
使用它来获取选定的单元格:
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
print(currentCell.textLabel!.text)
答案 2 :(得分:0)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// Do not forgot to set cell tag.
cell.tag = indexPath.row//cell tag has been set to indexpath.row
return cell
}
为了导航到你的tableViewController实现PrepareForSegue,如下所述。
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
if segue.identifier == "Your_Identifier" {
// Use this tag to pass data for the selected cell
//this tag will act as indexpath.row
guard let tag = (sender as? UIView)?.tag
else {
return
}
let vc = segue.destination as! ViewController// change ViewController with your view controller
vc.title = YOUR_VALUE // get title for selected row from your array using tag. and
//(vc.title) is the title of string type declared in next view controller
// add the data you want to parse here.
print(tag)
}
}
在下一个视图控制器
中您可以通过更改正在显示的视图控制器的标题来更改标题:
通常这是在视图中完成加载视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
self.title = title //title is the property created by you
}