Swift 3如何将cell对象数据用于另一个viewController

时间:2017-07-31 10:09:34

标签: ios swift uitableview segue

基本上,我正在尝试传递/使用我已经从Firebase获取的单元格实例(label.text)并将其分配给目标chatViewController个变量

我相信我遇到了segue的问题,在调试我的代码之后,segue没有以这种方式传递数据:

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

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

    cell.nameLblCell.text = users[indexPath.row].name
    cell.emailLblCell.text = users[indexPath.row].email
    cell.profilePictureCell.downloadImage(from: users[indexPath.row].proPicURL)
    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let instance = TableViewCell()
    let chatVc = segue.destination as? ChatViewController
    chatVc?.reciverImageVariable = instance.profilePictureCell.image!
    chatVc?.destinationEmail = instance.emailLblCell.text!
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let userId = users[indexPath.row].id
    self.performSegue(withIdentifier: "ShowChatView", sender: userId)

}

4 个答案:

答案 0 :(得分:0)

cellForRow(at: indexPath)会为您提供相应的单元格值

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
        print(cell.myTextField.text)
    }

如果您有UITableViewCell(预定义)使用 -

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        print(cell?.textLabel?.text)
    }

答案 1 :(得分:0)

您可以将tableview选择的索引存储在另一个变量中,也可以直接使用tablview选择的索引属性。简单的使用,您可以获得选定的单元格的实例。在preparForSegue方法中简单更改一个留置权。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  let instance = tableView.cellForRow(at: tableView.indexPathForSelectedRow) 
  let chatVc = segue.destination as? ChatViewController
  chatVc?.reciverImageVariable = instance.profilePictureCell.image!
  chatVc?.destinationEmail = instance.emailLblCell.text!
}

答案 2 :(得分:0)

为什么从tableviewcell获取数据,您可以从主对象users获取数据。

var selectedUserIndex = 0   

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let chatVc = segue.destination as? ChatViewController
    chatVc?.destinationEmail = users[selectedUserIndex].email
    chatVc?.reciverImageVariable = users[selectedUserIndex].proPicURL //again load from url in ChatViewController
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedUserIndex = indexPath.row
    self.performSegue(withIdentifier: "ShowChatView", sender: userId)    
}

答案 3 :(得分:0)

我找到了答案...如果ChatViewController嵌入在其自己的UINavigationController中,则segue.destinationViewController将是一个UINavigationViewController,然后我跟随@Pankaj一起发送了他发送给我的链接(Send data from TableView to DetailView Swift)所以最终结果如下:

var valueToPass:String! // #1 i added this one first 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as! TableViewCell

    valueToPass = currentCell.emailLblCell.text

    self.performSegue(withIdentifier: "ShowChatView", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // #2 used if let as? UINavigationController

    if let navigationController = segue.destination as? UINavigationController {
        let destinationVC = navigationController.topViewController as? ChatViewController
        destinationVC?.destinationEmail = valueToPass
    }