首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Swift 3如何将单元格对象数据用于另一个viewController

Swift 3如何将单元格对象数据用于另一个viewController
EN

Stack Overflow用户
提问于 2017-07-31 10:09:34
回答 4查看 1K关注 0票数 0

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

我相信我正面临着segue的问题,在调试我的代码后,segue不会以这种方式传递数据:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-07-31 13:01:26

我找到了答案。segue.destinationViewController将是一个UINavigationViewController,如果ChatViewController被嵌入到它自己的UINavigationController中,那么我跟随@Pankaj,并带有他发送给我的链接(Send data from TableView to DetailView Swift),所以最终结果如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
    }
票数 0
EN

Stack Overflow用户

发布于 2017-07-31 10:17:37

cellForRow(at: indexPath)将为您提供各自的单元格值

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
        print(cell.myTextField.text)
    }

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        print(cell?.textLabel?.text)
    }
票数 0
EN

Stack Overflow用户

发布于 2017-07-31 10:26:18

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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!
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45412986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文