【问题标题】:Get navigation bar title from selected row从选定行获取导航栏标题
【发布时间】:2014-12-29 01:49:55
【问题描述】:

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

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

title = self.restaurant.name 

但我还没有弄清楚如何在不创建额外标签的情况下将其传递给导航栏。

【问题讨论】:

    标签: uitableview swift title navigationbar


    【解决方案1】:

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

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多