Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data to a custom UITableViewCell class

Tags:

ios

swift

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.

like image 353
butter_baby Avatar asked Nov 26 '25 08:11

butter_baby


1 Answers

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
}
like image 58
Lirik Avatar answered Nov 28 '25 23:11

Lirik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!