-1

I'm using a custom cell of UITableViewCell in multiple tableview, and its working fine, but i want to know that, the custom cell is used by which tableview ?

Eg.

Suppose ViewControllerA has a tableView with custom cell, namely (ReuseIdentifier) cellA. Also ViewControllerB has a tableView with custom cell, namely (ReuseIdentifier) cellB

but both cell has the same class.

Now, from a ViewController both the class has push by any button action.

now, how that custom cell will understand which tableView is used it ?

Is there any way ?

Any answer will be appreciated

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • Take a look at delegate patterns, described here: https://useyourloaf.com/blog/quick-guide-to-swift-delegates/ – Tamás Sengel Jul 20 '17 at 15:24
  • You're right that it's fine for two vcs to each have a table that use the same cell class. Those cells may have buttons, and it's a little puzzle, even when there's a single vc/table, about how to convey the button action to the vc, which is where you typically want it to go (not the table!). Here's one of about a zillion articles on that (https://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell). – danh Jul 20 '17 at 15:56
  • @danh Partially you are right, but here my question is not about the button, I wanted to know the cell used by which class's tableview, how that cell become to know whom it was was used by which tableview ? – Abhishek Mitra Jul 21 '17 at 07:12
  • It's simple for a cell to know the tableview to which it belongs: Walk up the superview chain: `tmpView = tmpView.superview;` until you find a table: `[tmpView isKindOfClass:[UITableView self]]`. **But I don't recommend this**. Needing to know this probably indicates an error in the design. – danh Jul 21 '17 at 14:10

1 Answers1

0

You could implement a delegate for each cell in tableView(_ tableView: UITableView, cellForRowAt indexPath: UIIndexPath). The same can be done in ViewController which will receive callbacks from all cells that you assigned instance of ViewController to delegate property.

See example below, and excuse my typos, written without IDE.

YourCell.swift

protocol YourCellDelegate {
    func cellDidTapButton(_ cell: YourCell)
}

class YourCell {
    var delegate: YourCellDelegate?

    ...

    // Action linked to your cells button
    @IBAction func didTapButton(_ sender: Any) {
        delegate?.cellDidTapButton(_ cell: YourCell)
    }
}

ViewControllerA.swift

class ViewControllerA: UITableViewDataSource {
    var tableView: UITableView!

    ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: UIIndexPath) {
        if let cell = tableView.dequeueReusableCell(withIdentifier:"ReuseIdentifier", at: indexPath) as? YouCell {
            // Do cell configuration

            ...

            cell.delegate = self

        }
    }
}


extension UIViewControllerB: YourCellDelegate {
    func cellDidTapButton(_ cell: YourCell) {
        // Get indexPath of this cell
        if let indexPath = self.tableView.indexPathForRow(at:cell.center) {
             // Do action specific to this cell
        }
    }
}
Imoth
  • 53
  • 8
  • Thanks for your answer, but I'm not looking for cell button Action, My concerns is about to loading cell on calling UITableViewDelegate and datasource, at this time how that cell became to know which tableview using him ? – Abhishek Mitra Jul 21 '17 at 06:36
  • @AbhishekMitra In that case you can have a `UITableView` property in you cell class and in `tableView(_ tableView: UITableView, cellForRowAt indexPath: UIIndexPath)` just assign whichever `tableView` is passed to this function to the property in the cell. But why would you want the cell to know which `UITableView` presents it? – Imoth Jul 21 '17 at 12:17
  • @AbhishekMitra can you give more context to your problem? what are you trying to achieve that your cell needs to know which tableView presents it? – Imoth Jul 21 '17 at 12:51