0

I am stuck on this problem. I am downloading a json file from Firebase and successfully displaying in a TableView. That TableView has a custom cell with two labels. I have set up a segue link from the custom cell to a Detail ViewController. That works fine, but now I want to take the text content of the cell labels and send to a destination ViewController.

I have had no trouble doing this in the past, but am having trouble implementing this from the label.text in a custom cell.

I am using label tags (label1 and label2, although I may change that to the label names sometime).

The question is, how do I get the text contents from the two labels from the row selected and pass those to the DetailViewController? All my attempts so far have failed. Is there something I should be doing in the:

  valueToPass = currentCell?.textLabel?.text 

Here is the code:

  struct postStruct {
  let property : String!
  let propType : String!
  }

class TableViewController: UITableViewController {

var posts = [postStruct]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    let databaseRef = FIRDatabase.database().reference()

  databaseRef.child("users").queryOrderedByKey().observe(.childAdded, with: {
        snapshot in

        let snapshotValue = snapshot.value as? NSDictionary
        let property = snapshotValue!["property"] as? String

        let propType = snapshotValue!["type"] as? String

        self.posts.insert(postStruct(property: property, propType: propType), at: 0)
        self.tableView.reloadData()
    })
  }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
 }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

   let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].property

   let label2 = cell?.viewWithTag(2) as! UILabel
    label2.text = posts[indexPath.row].propType
  //  print(posts)
  //  cell.setcell(valueToPass.label1)
      return cell!
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

var valueToPass:String!

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow;
    let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!;

   valueToPass = currentCell?.textLabel?.text 
    performSegue(withIdentifier: "detailSegue", sender: self)
       }

Any help most welcome, in particular code examples. Many thanks in anticipation

PhilipS
  • 379
  • 5
  • 15

1 Answers1

1

Create a custom class for your tableCell, and attach outlets in the StoryBoard.

the cell creation in cellForRowAt becomes

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") As! MyCustomCell
cell.label1.text = posts[indexPath.row].property
cell.label2.text = posts[indexPath.row].propType

and then didSelectRowAtIndexPath becomes

let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as! MyCustomCell

self.valueToPass = currentCell.label1?.text 
performSegue(withIdentifier: "detailSegue", sender: self)

The final thing you need if to prepare for the segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "detailSegue" // you may have more than one segue, so you should check
    {
        let destinationController : DestinationViewControllerClass = segue.destination as! DestinationViewControllerClass
        destinationController.valueToPass = self.valueToPass
    }
}
Russell
  • 5,436
  • 2
  • 19
  • 27
  • Hi, and thanks for taking the time to respond. However, I am having a few issues. Should custom class be a subclass of UITableViewController or subclass of TableViewCell? Was unable to connect outlets in the TableViewCell. Should it be in the TableViewController? Am I correct in thinking the code you provided goes in the TableViewController? If so, I did get some errors when I tried this e.g. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") As! MyCustomCell gave an error of 'consecutive declarations on a line must be separated by ; ' – PhilipS Jan 26 '17 at 14:41
  • Probably best to take this off to the chat rooms - i have set one up called TableView ... – Russell Jan 26 '17 at 14:46
  • Hi, I wonder if you can help me out on another problem. I asked the question but have received no replies. Actually follows on from the last problem you kindly helped me with : http://stackoverflow.com/questions/41937700/swift-3-passing-var-through-a-series-of-segues-viewcontrollers – PhilipS Jan 31 '17 at 07:30
  • Sure - Lets go chat. I have renamed the meeting room we used before to `Swift Questions (Russell)` See you there? – Russell Jan 31 '17 at 08:22