2016-01-11 76 views
0

我想通過使用「performSegueWithIdentifier」將值從tableview傳遞給某個詳細視圖。詳細信息頁面運行良好,但值不正確顯示在屏幕上。如果我點擊tableview第一個選項,詳細頁面的內容爲空白。如果我點擊第二個項目,第一個加載詳細信息頁面。TableView和Detail Segue

我的實現代碼如下代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let object = objectAtIndexPath(indexPath) 
    self.titulo = object!.objectForKey("titulo") as! String 
    self.sub_titulo = object!.objectForKey("sub_titulo") as! String 
    self.url = object!.objectForKey("url") as! String 
    self.tipo = object!.objectForKey("tipo") as! Int 
    //Chama a segue 
    self.performSegueWithIdentifier("detailSegue", sender: self) 
} 

//In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let svc = segue.destinationViewController as! DetailViewController 
    svc.titulo = self.titulo 
    svc.sub_titulo = self.sub_titulo 
    svc.url = self.url 
    svc.tipo = self.tipo 
} 

我的DetailView控制器:

import UIKit 

class DetailViewController: UIViewController { 
    @IBOutlet weak var imagemDestaque: UIImageView! 
    @IBOutlet weak var tituloLabel: UILabel! 
    @IBOutlet weak var conteudoLabel: UILabel! 

    var titulo = String() 
    var sub_titulo = String() 
    var tipo = Int() 
    var url = String() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view.s 
     print(self.titulo) 
     tituloLabel.text = self.titulo 
     conteudoLabel.text = self.sub_titulo 
    } 
    } 

回答

0

改變這樣的代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //Chama a segue 
    self.performSegueWithIdentifier("detailSegue", sender: self) 
} 

//In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let svc = segue.destinationViewController as! DetailViewController 

    let indexPath = tableView.indexPathForSelectedRow() 
    let object = objectAtIndexPath(indexPath) 

    svc.titulo = object!.objectForKey("titulo") as! String 
    svc.sub_titulo = object!.objectForKey("sub_titulo") as! String 
    svc.url = object!.objectForKey("url") as! String 
    svc.tipo = object!.objectForKey("tipo") as! Int 
} 
+0

我想這個代碼,但問題是相同的。 = /如果我點擊tableview第一個選項,詳細頁面的內容爲空白。如果我點擊第二個項目,他們會加載詳細頁面中的第一個負載,放入正確的負載。 – Lavorato

+0

是否調試過'prepareForSegue'在那裏放置一個斷點,然後執行代碼 – meda