基本上,我尝试传递/使用已经从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)
}
发布于 2017-07-31 13:01:26
我找到了答案。segue.destinationViewController将是一个UINavigationViewController,如果ChatViewController被嵌入到它自己的UINavigationController中,那么我跟随@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
}
发布于 2017-07-31 10:17:37
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)
}
发布于 2017-07-31 10:26:18
您可以将tableview的选定索引存储在另一个变量中,也可以直接使用tableview的选定索引属性。简单地使用它,您可以获得选定单元格的实例。只需在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!
}
https://stackoverflow.com/questions/45412986
复制相似问题