简体   繁体   中英

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

我有一个自定义单元格有一个xib,这个单元格包含一个按钮,当按下按钮我想做一个动作,但不是在我的自定义单元格类中,而是从包含自定义单元格的tableview的viewcontroller内部,请帮忙?

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

    }
}

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...")
} }

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

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

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

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;
   //...
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM