Get data when tableview item clicked

Viewed 82

I have one TableView, and inside that I have another TableView, in which each cell contains a switch.

When I click on that switch, I want to send the data for that cell to my view controller. How can I achieve this?

Here is my approach:

I created a protocol from where I am sending position clicked from inner TableView to outer TableView, but I am not able to get the complete object, and I don't know how to proceed further.

Inner TableView cell switch clicked :

@IBAction func switchClicked(_ sender: UISwitch) {
//position clicked
   print("inside cell clicked>>\(sender.tag)")
    cellDelegate?.tableViewCellClicked(position: sender.tag, type: 
 "SwitchClicked")
}

Method call in outer TableView class :

func tableViewCellClicked(position: Int, type: Any) {
        print("position\(position)")
    }
1 Answers

You can pass a closure for your switch function, inside the cell have a

var switchAction: () -> Void? 

And inside the cell switch function call switchAction?(), Now while setting the cell set a switchAction for example

func foo() { } 
Cell.switchAction = foo

Now when u click switch action foo function will trigger you can pass w/e you want with that.

Related