UITableViewCell Buttons with action

前端 未结 7 972
无人共我
无人共我 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 00:54

    SWIFT 4.*

    It can be done like following way too, Not required much coding and delegation, Simple and easy.

    Put following code in cellForItemAt for UICollectionView or in cellForRowAt for UITableView

    cell.btn.tag = indexPath.row
    cell.btn.addTarget(self, action: #selector(buttonSelected), for: .touchUpInside)
    

    And your Method will be

    @objc func buttonSelected(sender: UIButton){
        print(sender.tag)
    }
    

    Thats all.

提交回复
热议问题