【问题标题】:TableView and Detail SegueTableView 和细节 Segue
【发布时间】:2016-01-11 00:35:57
【问题描述】:

我正在尝试使用“performSegueWithIdentifier”将值从表视图传递到某个详细视图。详细信息页面运行良好,但值未正确显示在屏幕上。如果我单击 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
    }
   }

【问题讨论】:

    标签: ios objective-c uitableview tableview segue


    【解决方案1】:

    像这样更改您的代码:

    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
    }
    

    【讨论】:

    • 我试过这段代码,但问题是一样的。 =/如果我点击tableview第一个选项,详细页面的内容为空白。如果我点击第二个项目,他们会在详细信息页面中加载第一个加载正确的项目。
    • 你调试了吗prepareForSegue 在那里放了一个断点,然后单步执行代码
    猜你喜欢
    • 2012-08-14
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多