0

basically, I'm trying to pass/use the cell instances (label.text) that I have already fetched from Firebase and assign it to the destination chatViewController variables

I believe I'm facing an issue with the segue ,, after debugging my code the segue do not pass the data this way:

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)

}
Phyber
  • 1,368
  • 11
  • 25
OT AMD
  • 183
  • 5
  • 19
  • in prepareforSegue, access the selected cell as let indexPath = tableView.indexPathForSelectedRow() let instance = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell – Jen Jose Jul 31 '17 at 10:12
  • `let instance = TableViewCell()` you are creating a whole new cell. Instead, use `tableView.selectedIndexPath()` to retrieve the indexPath, and then use `user[thatIndexPathSelected.row]`. – Larme Jul 31 '17 at 10:13
  • you have try with navigation controller @OT AMD – Berlin Jul 31 '17 at 10:22

4 Answers4

0

cellForRow(at: indexPath) will give you respective cell values

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

If you have UITableViewCell (predefined) use -

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        print(cell?.textLabel?.text)
    }
Jack
  • 13,571
  • 6
  • 76
  • 98
0

You can store tableview selected index in another variable or you can directly use tablview's selected index property. simple using that you can get selected cell's instance. simple change one lien in your preparForSegue method.

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!
}
Pankaj K.
  • 535
  • 8
  • 18
  • i got an error 'has no memebr type' after force unwrapping this: let instance = tableView.cellForRow(at: tableView.indexPathForSelectedRow) – OT AMD Jul 31 '17 at 10:34
  • have you unwrapped cell ? let newCell = tableView.cellForRow(at: tableView.indexPathForSelectedRow!) i have tested this and i can print cell value too. – Pankaj K. Jul 31 '17 at 10:50
  • after that i followed xcode and i got the syntax error – OT AMD Jul 31 '17 at 12:01
  • okay you don't need to pass data from tableView cell, just use indexPath.row in did select method. this is also helpful for you. https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift – Pankaj K. Jul 31 '17 at 12:21
  • so after that what really happens instance. doesnt auto complete to emailLblCell.text or doesnt recognize it – OT AMD Jul 31 '17 at 12:22
0

Why are you getting data from tableviewcell you can get data from main object 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)    
}
Pramod Tapaniya
  • 1,228
  • 11
  • 30
  • I recommend load image in next screen from url, because it is possible you have tapped on the cell, but the image was not loaded in that situation image will pass nil. – Pramod Tapaniya Jul 31 '17 at 11:13
  • i used your solutions no build errors so far but when i print the value (email, imgStr) nothing appears i think it doesnt pass the data .... in the chatviewcontoller my variables are: var destinationEmail = String() var reciverImageVariable = String() so i test it using print(destinationEmail) in the viewDidload() – OT AMD Jul 31 '17 at 11:40
  • print variable into prepareForSegue method. – Pramod Tapaniya Jul 31 '17 at 11:47
  • print(chatVc?.destinationEmail) – Pramod Tapaniya Jul 31 '17 at 11:50
  • i got nil,,,, actually i intended to print it in the second view controller (chatVC) but nothing shows – OT AMD Jul 31 '17 at 11:59
  • i know you want to print into chatVC. now write this code into prepareForSegue method print(users[selectedUserIndex].email) – Pramod Tapaniya Jul 31 '17 at 12:02
  • @OTAMD do one thing, take one var like int or NSInteger and store indexPath.row in that var when you click on tableview cell. now in prepareForSegue method use that var and get selected object from your array and print that. if you successfully print out that data then pass that data to next viewController. print this one users[selectedUserIndex] , also add structure of your array so we can check it. – Pankaj K. Jul 31 '17 at 12:04
  • @PramodTapaniya yes you have done that but still he is not able to print that so need to check array structure – Pankaj K. Jul 31 '17 at 12:07
  • @PramodTapaniya i finally got the result printed in the debug area when segue is performed but its not printed in the chatVC so can be able to use ? – OT AMD Jul 31 '17 at 12:07
  • this is my user data: `internal class User { internal let id: String internal let name: String internal let email: String internal let proPicURL: String init(id: String, name: String, email:String, proPicURL:String) { self.id = id self.name = name self.email = email self.proPicURL = proPicURL } }` – OT AMD Jul 31 '17 at 12:10
  • check segue destination viewcontroller and it's class, name of segue. – Pramod Tapaniya Jul 31 '17 at 12:13
  • @OTAMD than accept as a correct answer. So other developers can easily find a solution. :) – Pramod Tapaniya Aug 05 '17 at 06:14
0

i found the answer ... the segue.destinationViewController will be a UINavigationViewController if ChatViewController is embedded in it's own UINavigationController then i followed along with @Pankaj with link he sent me (Send data from TableView to DetailView Swift) so final result as follow:

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
    }
OT AMD
  • 183
  • 5
  • 19