简体   繁体   中英

TableView and Detail Segue

I'm trying to pass values from an tableview to some detail view using the "performSegueWithIdentifier". The detail page works well but the values are not coming correctly to the screen. If I click in tableview first option, the content of detail page in blank. If I click in the second item, the first one load in the detail page.

My tableview code:

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
}

My detailview controller:

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

Change your code like this:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM