【问题标题】:How to passing Date between two Storyboard with didSelectRowAtIndexPath(in Swift)?如何使用 didSelectRowAtIndexPath(在 Swift 中)在两个 Storyboard 之间传递日期?
【发布时间】:2015-08-06 09:53:36
【问题描述】:

我需要使用didSelectRowAtIndexPath 才能通过 搜索后我当前的单元格(这是我找到的唯一方法), 但是如何将此单元格名称传递给 SecondViewController?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath  indexPath: NSIndexPath) {

   if(tableView ==   self.searchDisplayController!.searchResultsTableView){
    if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
        if let cellTextLabel: UILabel = cell.textLabel{                         
           let  cellSelected = cellTextLabel.text!   

   }
  }
}

在 SecondViewController 中,我需要使用 TextView 将当前文本呈现给选定的单元格。

【问题讨论】:

    标签: ios xcode swift storyboard didselectrowatindexpath


    【解决方案1】:

    试试这个代码:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath  indexPath: NSIndexPath) {
    
       if(tableView ==   self.searchDisplayController!.searchResultsTableView){
        if let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath){
            if let cellTextLabel: UILabel = cell.textLabel{                         
               let  cellSelected = cellTextLabel.text!  
               // pass your segue name
               self.performSegueWithIdentifier("segueToDetailVC", sender: cellTextLabel.text!)
       }
      }
    }
    

    传递数据

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if (segue.identifier == "segueToDetailVC")
            {
                var str: String = sender as! String
                // your ViewController class object
                var detail: DetailVC = segue.destinationViewController as! DetailVC
                // pass text in String Variable
                detail.text = str
            }
        }
    

    【讨论】:

    • 我得到 Fehler “无法分配 UITextView 类型的 String zo 类型的值”,你有什么想法吗?
    • Detail.text 中的费勒派 = str
    【解决方案2】:

    在目标视图控制器中添加一个属性secondViewController,并使用prepareForSegue

       override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
          if (segue.identifier == "segueTest") {
               var svc = segue!.destinationViewController as secondViewController;
    
               svc.toPass = cellTextLabel.text
    
           }
        }
    

    在 secondViewController 中你可以定义一个像 String 这样的变量

    var toPass:String!
    

    在第二个ViewController的viewDidLoad函数下添加这段代码

    println(\(toPass))
    

    【讨论】:

    • 问题是这种方式是不可行的,因为当我使用搜索时我的单元格树发生了变化,这对于使用单元格 textLabel.text 是不可用的。
    • 这是我在 dem 链接中的代码,谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多