gpt4 book ai didi

uitableview - 从选定行获取导航栏标题

转载 作者:搜寻专家 更新时间:2023-11-01 06:10:01 25 4
gpt4 key购买 nike

我创建了一个 TableView ,用户可以通过单击导航栏中的“添加”按钮来添加行。当用户选择一行时,应用程序会显示另一个表格 View 。我想将导航栏的标题设置为所选行的名称。

如果我将名称传递给第二个 TableView 中的标签,我知道如何设置导航栏的标题。

title = self.restaurant.name 

但我还没有想出如何在不创建额外标签的情况下将其传递到导航栏。

最佳答案

其实很简单。您所要做的就是实现 prepareForSegue 并使用 sender 创建 UITableViewCell 的实例。从那里您可以轻松获得该单元格的标题,并使用 segue.destinationViewController 您可以设置后续 View Controller 的导航栏标题。

import Foundation
import UIKit

class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

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

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell

cell.textLabel?.text = "Cell at row \(indexPath.row)"

return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as UIViewController
let cell = sender as UITableViewCell
destinationVC.navigationItem.title = cell.textLabel?.text
}
}

关于uitableview - 从选定行获取导航栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678801/

25 4 0