Ios 未在detailview中接收数据

Ios 未在detailview中接收数据,ios,swift,uitableview,swift3,detailview,Ios,Swift,Uitableview,Swift3,Detailview,我有一个TableView,当用户单击它时,它需要显示一个详细视图,显示它单击的行的名称。我使用Json调用填充tableView,但我不知道我做错了什么 这是我的ViewController代码 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var valueToPass:String! @IBOutlet weak var

我有一个TableView,当用户单击它时,它需要显示一个详细视图,显示它单击的行的名称。我使用Json调用填充tableView,但我不知道我做错了什么

这是我的ViewController代码

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var valueToPass:String!

    @IBOutlet weak var tableView: UITableView!
    // Instantiate animals class
    var items = [animalObject]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        addDummyData()
    }

    func addDummyData() {
        RestManager.sharedInstance.getRandomUser(onCompletion: {(json) in
            if let results = json.array {
                for entry in results {
                    self.items.append(animalObject(json: entry))
                }
                DispatchQueue.main.sync{
                    self.tableView.reloadData()
                }
            }
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! CustomTableViewCell!

        let animal = self.items[indexPath.row]

        cell?.label.text = animal.name

        return cell! //4.
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.row)!")

        // Get Cell Label
//        let indexPath = tableView.indexPathForSelectedRow!
        var currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
        print(currentCell.textLabel?.text)
        valueToPass = currentCell.textLabel?.text
        print(valueToPass)
        performSegue(withIdentifier: "detailView", sender: indexPath)
    }

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

        if (segue.identifier == "detailView") {
            // initialize new view controller and cast it as your view controller
            let viewController = segue.destination as! DetailViewController
            // your new view controller should have property that will store passed value
            viewController.passedValue = valueToPass
        }
    }

}
我的detailView只有以下信息

@IBOutlet weak var tooPass: UILabel!
var passedValue: String!

override func viewDidLoad() {
    super.viewDidLoad()
    print("DETAILS")
    print(passedValue)
    tooPass.text = passedValue
}
我不确定preparedToSegue是否提前一点启动,因为我的终端看起来如下:


我将以下任何指导用作参考,不胜感激

请尝试此方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        valueToPass = self.items[indexPath.row]
        print(valueToPass)
        performSegue(withIdentifier: "detailView", sender: indexPath)
    }
只需在items数组中传递整个项


将DetailViewController传递的项设置为item类型。然后只需访问
项。所需的任何属性

问题是,在
cellForRow
方法中,您将单元格强制转换为
CustomTableViewCell
类型,而在
didSelectRow
中,您将其强制转换为
UITableViewCell
。改变

var currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
在您的
中,选择Row

var currentCell = tableView.cellForRow(at: indexPath)! as CustomTableViewCell

这里的好方法是使用视图模型。创建包含项目和与之关联的数据的模型。在segue传递您的数据模型。在didSelect方法上正确地从单元格中获取数据是糟糕的编码,除非某些用户界面在该单元格上工作

您不需要
让indexPath=tableView.indexPathForSelectedRow。您已经可以在
didSelectRow
方法中访问
indepath
。拆下这条线,看看是什么happens@Malik它没有改变任何东西你能分享你的
cellForRow
方法吗?检查currentCell的值,或者currentCell.textLabel..它不是swift@Malikits预先定义的方法,访问数组比从didSelectRowAt方法中调用cellForRow方法更好。它可以工作,但我只是阅读了我的问题,我没有很好地解释我试图实现的目标,用户单击单元格后,我需要传递完整的单元格,因为在详细信息视图中,我需要显示.name和.species。对不起,这是我的错inconvenience@user2737948我相信,您应该在self.items数组中包含名称和种类。只需在数组中传递整个项,当我在detailViewController中展开它时,它一直接收一个nil值,我将字符串正确地更改为animalObject。错误:在展开可选文件时意外发现nilvalue@user2737948在prepareForSegue中放置一个断点,查看所传递的值是否不是nil。。并检查详细视图控制器的内部视图加载