UITableViewCell Buttons with action

前端 未结 7 944
无人共我
无人共我 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:48

    You can also get selected button index using tableViewCell view's hierarchy.

    Using following steps :

    1. add selector to the cellForRowAtIndexpath of tableview :

      btn?.addTarget(self, action:#selector(buttonPressed(_:)), for:.touchUpInside)
      

    2 . get indexPath using following method :

    func buttonPressed(_ sender: AnyObject) {
            let button = sender as? UIButton
            let cell = button?.superview?.superview as? UITableViewCell
            let indexPath = tblview.indexPath(for: cell!)
            print(indexPath?.row)
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 01:00

    I was resolving this using a cell delegate method within UITableViewCell's subclass.

    Quick overview:

    1) Create a protocol

    protocol YourCellDelegate : class {
        func didPressButton(_ tag: Int)
    }
    

    2) Subclass your UITableViewCell (if you haven't done so):

    class YourCell : UITableViewCell
    {
         var cellDelegate: YourCellDelegate?   
          @IBOutlet weak var btn: UIButton!
        // connect the button from your cell with this method
        @IBAction func buttonPressed(_ sender: UIButton) {
            cellDelegate?.didPressButton(sender.tag)
        }         
        ...
    }
    

    3) Let your view controller conform to YourCellDelegate protocol that was implemented above.

    class YourViewController: ..., YourCellDelegate {  ... }
    

    4) Set a delegate, after the cell has been defined (for reusing).

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCell
    cell.cellDelegate = self
    cell.btn.tag = indexPath.row
    

    5) In the same controller (where is your implemented UITableView delegate/datasource), put a method from YourCellDelegate protocol.

    func didPressButton(_ tag: Int) {
         print("I have pressed a button with a tag: \(tag)")
    }
    

    Now, your solution is not tag / number dependent. You can add as many buttons as you want, so you are ready to get response via delegate regardless how many buttons you want to install.

    This protocol-delegate solution is preferred in iOS logic and it can be used for other elements in table cell, like UISwitch, UIStepper, and so on.

    0 讨论(0)
  • 2020-12-01 01:01

    UIButton in Tableview cell. Programically create action method in swift 4.2

    cell.btnlike.addTarget(self, action: #selector(buttonbtnlikePressed(_:event:)), for: .touchUpInside)
    
    
    @objc func buttonbtnlikePressed(_ sender: Any, event: Any) {
            let point : CGPoint = (sender as AnyObject).convert(CGPoint.zero, to:tblPost)
            var indexPath =  self.tblPost!.indexPathForRow(at: point)
            if let btnlike = sender as? UIButton{
                if btnlike.isSelected{
                    btnlike.isSelected = false
                }else{
                    btnlike.isSelected = true
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-01 01:07

    I came across the same problem after making the IBOutlets private as has been broadly suggested by the community.

    Here is my solution:

    < In your cell class >

    protocol YourCellDelegate: class {
        func didTapButton(_ sender: UIButton)
    }
    
    class YourCell: UITableViewCell {
    
        weak var delegate: YourCellDelegate?
    
        @IBAction func buttonTapped(_ sender: UIButton) {
            delegate?.didTapButton(sender)
        }
    }
    

    < In your ViewController >

    class ViewController: UIViewController, YourCellDelegate {
    
        func didTapButton(_ sender: UIButton) {
            if let indexPath = getCurrentCellIndexPath(sender) {
                item = items[indexPath.row]
            }
        }
    
        func getCurrentCellIndexPath(_ sender: UIButton) -> IndexPath? {
            let buttonPosition = sender.convert(CGPoint.zero, to: tableView)
            if let indexPath: IndexPath = tableView.indexPathForRow(at: buttonPosition) {
                return indexPath
            }
            return nil
        }
    }
    
    0 讨论(0)
  • 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 
      }
    }
    
    0 讨论(0)
提交回复
热议问题