【问题标题】:Swift 3 how to use cell objects data to another viewControllerSwift 3 如何使用单元格对象数据到另一个 viewController
【发布时间】:2017-07-31 10:09:34
【问题描述】:

基本上,我正在尝试传递/使用我已经从 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)

}

【问题讨论】:

  • 在prepareforSegue中,访问选中的单元格为 let indexPath = tableView.indexPathForSelectedRow() let instance = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
  • let instance = TableViewCell() 你正在创建一个全新的单元格。相反,使用tableView.selectedIndexPath() 检索indexPath,然后使用user[thatIndexPathSelected.row]
  • 你已经尝试使用导航控制器@OT AMD

标签: ios swift uitableview segue


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以将 tableview 选择的索引存储在另一个变量中,也可以直接使用 tablview 的 selected index 属性。简单地使用它,您可以获得选定单元格的实例。只需在您的 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!
    }
    

    【讨论】:

    • 强制解包后出现错误“没有 memebr 类型”:let instance = tableView.cellForRow(at: tableView.indexPathForSelectedRow)
    • 你打开了单元格吗? let newCell = tableView.cellForRow(at: tableView.indexPathForSelectedRow!) 我已经测试过了,我也可以打印单元格值。
    • 之后我跟着xcode,我得到了语法错误
    • 好的,您不需要从 tableView 单元格传递数据,只需在 do select 方法中使用 indexPath.row。这对你也有帮助。 stackoverflow.com/questions/28430663/…
    • 所以在那之后真正发生了什么。不会自动完成到 emailLblCell.text 或无法识别它
    【解决方案3】:

    为什么你从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)    
    }
    

    【讨论】:

    • 我建议在下一个屏幕中从 url 加载图像,因为您可能点击了单元格,但在这种情况下未加载图像图像将通过 nil。
    • 我使用了你的解决方案,到目前为止没有构建错误,但是当我打印值(电子邮件,imgStr)时,我认为它没有传递数据....在chatviewcontoller中我的变量是:var destinationEmail = String() var reciverImageVariable = String() 所以我在 viewDidload() 中使用 print(destinationEmail) 测试它
    • 将变量打印到 prepareForSegue 方法中。
    • 打印(chatVc?.destinationEmail)
    • 我得到了 nil,,,, 实际上我打算在第二个视图控制器 (chatVC) 中打印它但没有显示
    【解决方案4】:

    我找到了答案......如果 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
        }
    

    【讨论】:

    • @Pankaj 感谢您在评论中分享的链接,这是最终结果的一部分
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2014-10-18
    • 2022-01-08
    • 2016-10-31
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多