0

I have a custom expandable UITableViewCell. When expanded, it shows a couple of UITextFields. I want to update the table-data in the UITableView after the value of a UITextField has changed. Detecting the change-events is not a problem.

Is there an easy way to access the custom UITableView controller from the UITableViewCell? Should I create a reference when creating the cell in cellForRowAtIndexPath?

Thanks!

klaaskox
  • 329
  • 4
  • 15
  • possible duplicate of [How to get UITableView from UITableViewCell?](http://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell). Although that question is Objective-C, converting Objective-C codes to swift should be no problem. – M. Porooshani Sep 07 '15 at 09:38

2 Answers2

1

I had the same need. I wanted to reload the tableview from a custom cell with

self.tableView.reloadData()

where self refers to the UITableViewController.

The easiest way I found was to use the Delegate pattern.

class MyTableVC:MyProtocol{
...
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  ...
  (cell as! MyCell).delegate   =   self
  ...
  }

  func reloadData() {
    self.tableView.reloadData()
  }
....
}

My custom cell and its protocol declaration

protocol MyProtocol : NSObjectProtocol {
    func reloadData()
}

class MyCell: UITableViewCell{
 ...
  weak var delegate: MyProtocol?

// When the job is done call reloadData()
  self.delegate?.reloadData()
}
Jan ATAC
  • 1,212
  • 1
  • 18
  • 36
0

In the above answer, in third line, you are comparing a UIView with a UIViewController object.

  if (view!.isKindOfClass(CustomTableViewController)) {

Try this,

 if (view!.isKindOfClass(CustomTableViewController.view)) {
Shubham Ojha
  • 461
  • 5
  • 17