How to perform action for a custom UITableViewCell

2019-07-15 23:19发布

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?

5条回答
祖国的老花朵
2楼-- · 2019-07-15 23:44

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楼-- · 2019-07-15 23:45

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;
   //...
}
查看更多
家丑人穷心不美
4楼-- · 2019-07-15 23:50

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

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

func buttonAction(sender: UIButton){
   //...
} 
查看更多
不美不萌又怎样
5楼-- · 2019-07-15 23:56

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

    }
}
查看更多
家丑人穷心不美
6楼-- · 2019-07-15 23:57

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.

查看更多
登录 后发表回答