【问题标题】:Not receiving data in detailview未在详细视图中接收数据
【发布时间】:2018-05-07 23:31:38
【问题描述】:

我有一个 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 是否早一点触发,因为我的终端看起来像这样:

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

【问题讨论】:

  • 你不需要let indexPath = tableView.indexPathForSelectedRow!。您已经可以在didSelectRow 方法中访问indexPath。删除这一行,看看会发生什么
  • @Malik 它没有改变任何东西
  • 你能分享你的cellForRow方法吗?
  • 检查 currentCell 的值,或者 currentCell.textLabel..
  • 这不是一种方法,而是 swift @Malik 预定义的一种方法

标签: ios swift uitableview swift3 detailview


【解决方案1】:

试试这个didSelectRowAt 方法。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        valueToPass = self.items[indexPath.row]
        print(valueToPass)
        performSegue(withIdentifier: "detailView", sender: indexPath)
    }

只需传递 items 数组中的整个项目。

将 DetailViewController 的传递项设为 item 类型。然后只需访问item.whateverPropertyRequired

【讨论】:

  • 它有效,但我刚刚阅读了我的问题,并没有很好地解释我要完成的工作,一旦用户单击单元格,我需要传递完整的单元格,因为在详细信息视图中我需要显示 .name 和 .species。很抱歉给您带来不便
  • @user2737948 我相信您应该在 self.items 数组中包含名称和物种。只需传递数组中的整个项目
  • 当我在 detailViewController 中解开它时,它一直收到一个 nil 值,我将它从 String 正确更改为 animalObject。错误:在展开可选值时意外发现 nil
  • @user2737948 在 prepareForSegue 中放置一个断点,看看传递的值是否不是 nil .. 并检查细节视图控制器的 viewDidLoad 内部
【解决方案2】:

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

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

在您的didSelectRow

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

【讨论】:

  • 访问数组比在 didSelectRowAt 方法中调用 cellForRow 方法更好
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2014-07-12
    • 2020-10-30
    • 1970-01-01
    • 2013-01-31
    相关资源
    最近更新 更多