I have a custom cell that has a xib, this cell contains a button, when the button is pressed I want to do an action , but not inside my custom cell class but from inside the viewcontroller that contains the tableview of the custom cell, any help please?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Unable to process app at this time due to a genera
- Enum with associated value conforming to CaseItera
You can use delegate pattern. Create a custom protocol.
and on tableview cell conform delegate
table view data source
conform protocol(delegate) from table view controller or view controller
Add action for button from
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
method from your viewController.Write selector function in your view Controller:
Just get the
UIButton
in yourcellForRowAtIndexpath
. Then write the following code.First of all you should write a protocol for example:
Then inside your custom cell class declare:
and inside your IBAction in the custom cell class:
Now in your viewController:
1- Make your view controller implement CustomCellDelegate. 2- In your cellForRow when declaring the cell don't forget to write:
3- Finally call the function in your viewcontroller:
In your cell define a protocol:
and define a delegate variable:
Make your
viewController
conform to the defined protocol.When creating the cell in the
viewController
, assign it the delegate:When the button in the cell is tapped, send the delegate appropriate message:
In your
viewController
implement the method:You can pass as an argument the cell's index, so the
viewController
knows which cell button was tapped.