TableView和细节Segue

时间:2016-01-11 00:35:57

标签: ios objective-c uitableview tableview segue

我正在尝试使用“performSegueWithIdentifier”将值从tableview传递到某个详细视图。详细信息页面运行良好,但值未正确显示在屏幕上。如果我在tableview第一个选项中单击,则详细页面的内容为空白。如果我单击第二个项目,第一个加载在详细信息页面中。

我的tableview代码:

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
}

我的详细视图控制器:

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

1 个答案:

答案 0 :(得分: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
}