UITableViewCell Buttons with action

前端 未结 7 969
无人共我
无人共我 2020-12-01 00:27

Hi I have a custom UITableViewCell with three buttons to handle a shopping cart function, Plus,Minus and Delete button and I need to know which cell has been touched.

7条回答
  •  眼角桃花
    2020-12-01 01:08

    swift 4.2

    You can also use closures instead of delegates

    1) In your UITableViewCell :

     class ExampleCell: UITableViewCell {
        //create your closure here  
             var buttonPressed : (() -> ()) = {}
    
            @IBAction func buttonAction(_ sender: UIButton) {
        //Call your closure here 
                buttonPressed()
            }
        }
    

    2) In your ViewController

    class ViewController:  UIViewController,  UITableViewDataSource, UITableViewDelegate {
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCell", for: indexPath) as! ExampleCell
       cell.buttonPressed = {
              //Code
               }
    return cell 
      }
    }
    

提交回复
热议问题