butter_baby
butter_baby

Reputation: 888

Passing data to a custom UITableViewCell class

ViewControllerA will be pushing to ViewControllerB. ViewcontrollerB is a UITableView that has a custom UITableViewCell that is registered upon load.

Here is my custom UITableViewCell:

override func awakeFromNib() {
        super.awakeFromNib()
    // Here is me attempting to pass my data from my view controller
    someButton.text = stringFromViewController
}

Here's my UITableView:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let customCell = CustomCell()
    customCell.stringFromViewController = "Some Text"
    self.performSegueWithIdentifier("ViewControllerB", sender: nil)    
}

Whenever I attempt this the string comes back as empty. I'm not sure if this has to do with the fact that I am trying to pass this data to the UITableViewCell instead of the UITableView that contains the custom cell. I'd like to pass the data to my custom cell class.

Upvotes: 3

Views: 4539

Answers (5)

Cjay
Cjay

Reputation: 1093

I agree with @Lirik

have a property observer on you controllerB and reload the table view, then in your cellForRowAtIndexPath(::)

cast the dequeued cell to your custom cell type so that you can access all the function of your cell

let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! CustomCell

Upvotes: 0

Cjay
Cjay

Reputation: 1093

I agree with @Lirik

have a property observer on you controllerB's property and reload the table view, then in your cellForRowAtIndexPath(::) cast the dequeued cell to your custom cell type so that you can access all the function of your cell

let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! CustomCell

cell.someBotton.text = stringFromViewControllerA

Upvotes: 0

Chris
Chris

Reputation: 147

My understanding of your question is that clicking on a row from View Controller A will push View Controller B and you want to pass some data to the custom cell in View Controller B. you have to use prepareForSegue to pass the data.

in view controller b, set a variable, and in your case, a bool.

class ViewControllerB {

var boolValue: Bool?

}

then in didSelectRowAtIndexPath in ViewControllerA you perform the segue

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

self.performSegueWithIdentifier("ViewControllerB", sender: nil)    
}

Then you have to perform prepareForSegue according to the row being selected

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if segue.identifier == "ViewControllerB" {
        //if you need to know which row is select, use indexPath.row
        if let indexPath = tableView.indexPathForSelectedRow {
            let controller = segue.destinationViewController as! ViewControllerB
            //send the details that you want, i.e
            if indexPath.row < 5 {
                controller.boolValue = true
            } else {
                controller.boolValue = false
            }
        }
    }

once the segue is called, you pass the data through prepareForSegue, then you can pass the information to the cell using cellForRowAtIndexPath method.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomTableViewCell
        //pass the boolValue from viewcontrollerB to anotherBoolValue in custom cell
        cell.anotherBoolValue = boolValue

    // Configure the cell...

    return cell
}

Upvotes: 0

Sucharu Hasija
Sucharu Hasija

Reputation: 1136

Because awakeFromNib() initilizes even before you initilize the cell.

instead of assigning it in this method use this in didSelectRowAtIndexPath

let customCell = CustomCell()

customCell.someButton.setText("Some Text", forState: .Normal)

self.performSegueWithIdentifier("ViewControllerB", sender: nil) 

Upvotes: 0

Lirik
Lirik

Reputation: 3197

You going about this the wrong way. your tableView is reloading automatically upon loading controller B. you should pass the data to the cell in tableView:CellForRow: method. you can also call tableView.reloadData() once you assign the value from controller A. here's an example:

class ControllerB {
  var tableView: UITableView! //IBOutlet?
  var stringFromA: String? {
      didSet {
        self.tableView.reloadData()
      }
 }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
    cell.stringFromA = self.stringFromA
    return cell
}

Upvotes: 1

Related Questions