How to perform action for a custom UITableViewCell inside my viewController?

孤者浪人 提交于 2020-01-03 19:38:51

问题


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?


回答1:


First of all you should write a protocol for example:

protocol CustomCellDelegate {
  func doAnyAction(cell:CustomUITableViewCell)
}

Then inside your custom cell class declare:

weak var delegate:CustomCellDelegate?

and inside your IBAction in the custom cell class:

@IBAction func onButtonTapped(_ sender: UIButton) {
    delegate?.doAnyAction(cell: self)
 //here we say that the responsible class for this action is the one that implements this delegate and we pass the custom cell to it.
}

Now in your viewController:

1- Make your view controller implement CustomCellDelegate. 2- In your cellForRow when declaring the cell don't forget to write:

cell.delegate = self

3- Finally call the function in your viewcontroller:

func doAnyAction(cell: CustomUITableViewCell) {
    let row = cell.indexPath(for: cell)?.row
  //do whatever you want

    }
}



回答2:


You can use delegate pattern. Create a custom protocol.

protocol CustomTableViewCellDelegate {
func buttonTapped() }

and on tableview cell conform delegate

class CustomTableViewCell: UITableViewCell {
var delegate: CustomTableViewCellDelegate!

@IBAction func buttonTapped(_ sender: UIButton) {
    delegate.buttonTapped()
} }

table view data source

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CustomTableViewCell
    cell.delegate = self
    return cell
}

conform protocol(delegate) from table view controller or view controller

extension TestViewController: CustomTableViewCellDelegate {
func buttonTapped() {
    print("do something...")
} }



回答3:


Just get the UIButton in your cellForRowAtIndexpath. Then write the following code.

button.addTarget(self, action:#selector(buttonAction(_:)), for: .touchUpInside). 

func buttonAction(sender: UIButton){
   //...
} 



回答4:


Add action for button from tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) method from your viewController.

cell.btn.tag = indexPath.row;

cell.btn.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside).  

Write selector function in your view Controller:

func handleButtonClicked(sender: UIButton){
     int cellofClickedbutton = sender.tag;
   //...
}



回答5:


In your cell define a protocol:

protocol MyCellDelegate: class {
    func buttonTapped()
}

and define a delegate variable:

weak var delegate: MyCellDelegate?

Make your viewController conform to the defined protocol.

When creating the cell in the viewController, assign it the delegate:

cell.delegate = self

When the button in the cell is tapped, send the delegate appropriate message:

delegate?.buttonTapped()

In your viewController implement the method:

func buttonTapped() {
    //do whatever you need
}

You can pass as an argument the cell's index, so the viewController knows which cell button was tapped.



来源:https://stackoverflow.com/questions/43627355/how-to-perform-action-for-a-custom-uitableviewcell-inside-my-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!