PhilipS
PhilipS

Reputation: 379

sending customCell label text to destination view

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

Upvotes: 0

Views: 246

Answers (1)

Russell
Russell

Reputation: 5554

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

Upvotes: 1

Related Questions