来自UITableViewCell的navigationItem.title

时间:2018-03-24 14:03:33

标签: ios swift uitableview uinavigationbar uinavigationitem

导航栏标题未根据所选单元格显示。

我在导航视图中有导航视图控制器。 容器视图位于ViewController中。

的ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "embedseg" {

        guard let splitViewController = segue.destination as? UISplitViewController,
            let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
            let masterViewController = leftNavController.topViewController as? MasterViewController,
            //let detailViewController = splitViewController.viewControllers.last as? DetailViewController
            let rightNavController = splitViewController.viewControllers.last as? UINavigationController,
            let detailViewController = rightNavController.topViewController as? DetailViewController

            else { fatalError() }

        let firstMonster = masterViewController.monsters.first
        detailViewController.monster = firstMonster

        masterViewController.delegate = detailViewController

        detailViewController.navigationItem.leftItemsSupplementBackButton = true
        detailViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem

        //detailViewController.navigationItem.title = "Hello" <-- tested this, it worked.

        //detailViewController.navigationItem.title = cell.textLabel?.text <-- this didn't work.

        //detailViewController.navigationItem.title = monster.name <-- and this didn't work.                        
    }
}

MasterViewController:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let monster = monsters[indexPath.row]
    cell.textLabel?.text = monster.name

    return cell
}

2 个答案:

答案 0 :(得分:0)

选择单元格时

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     let cell = tableView.cellForRow(at: indexPath) as! YourCellClassName

     self.performSegue(withIdentifier: "embedseg", sender:cell.textLabel?.text)
}

在prepareforSegue

let cellText = sender as! String

detailViewController.navigationItem.title = cellText 

答案 1 :(得分:0)

这是因为monstercell是在

中定义的
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}

但不在里面

override func prepare(for segue: UIStoryboardSegue, sender: Any?){}

如果您使用故事板设置了segue,则可以执行以下操作:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if let cell = sender as? UITableViewCell {
        detailViewController.navigationItem.title = cell.textLabel?.text
    }
}

或类似的东西:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if let cell = sender as? UITableViewCell {
        let indexPath = tableView.indexPath(for: cell)
        detailViewController.navigationItem.title = monsters[indexPath.row].name
    }
}

在第一种情况下,您将“sender”转换为单元格,以便您可以访问它的值,在第二种情况下,您将“sender”作为单元格,您可以找到它的indexPath,然后使用indexPath查找使用数组monsters

更正“怪物”