0

I have two types of custom cells in my tableview and I want to find out which type of cell is selected when the user taps on a particular row. What should I write in didSelect delegate of UITableView?

Pooja Mishra
  • 17
  • 1
  • 8
  • 1
    Possible duplicate of [How do you find out the type of an object (in Swift)?](https://stackoverflow.com/questions/24101450/how-do-you-find-out-the-type-of-an-object-in-swift) – dahiya_boy Feb 06 '18 at 07:16
  • 1
    Possible duplicate of [Detect when a custom cell is selected from within the cell itself?](https://stackoverflow.com/questions/41581820/detect-when-a-custom-cell-is-selected-from-within-the-cell-itself) – Jayesh Thanki Feb 06 '18 at 07:16
  • How you decide which custom cell need to display? – Indrajeet Feb 06 '18 at 07:20

1 Answers1

0

If there are two custom cells then there must be two Classes of UITableViewCell, you can simply differentiate the cell based on that . See the code below :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell1 {
        //Perform action here
    } else if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell2 {
        //Perform action here
    }
}
Madhur
  • 1,056
  • 9
  • 14