which cell button has pressed in tableView in swift 3

前端 未结 4 590
别那么骄傲
别那么骄傲 2021-02-11 07:53

I have read similar questions like this but these codes didn\'t worked for me so please help : I have a table view with some buttons in each cells - This table view is factor fo

相关标签:
4条回答
  • 2021-02-11 08:42
      Here is update
    
    just leave it, i explained it more simple,
    create a button action in your cell class,then
    Writre the code:-
    
    > //Cell class
    
    protocol CellDelegate: class {
        func didTapCell(index: IndexPath)
    }
      var delegateCell:CellDelegate?
      var indexPath:IndexPath?
     @IBAction func buttonCellClick(_ sender: Any) {
            delegateCell?.didTapCell(index: indexPath!)
        }
    
    > //In ViewController
    
     cell.delegateCell = self
     cell.indexPath = indexPath
    
     func didTapCell(index: IndexPath) {
            print("PRESS BUTTON >> \(index.row)")
        }
    
    0 讨论(0)
  • 2021-02-11 08:53

    You are setting factor button's tag in your cellForRowAt so you can easily get indexpath from your factorViewController.Bill, and from that indexpath you can get your whole cell and you can do whatever you need.

    You can get cell like,

     let cell = tableView.cellForRowAtIndexPath(indexPath)
    

    you can refer this so post to know how to get indexpath from button click!

    0 讨论(0)
  • 2021-02-11 08:54

    When you press factorBillbutton then your Bill method will call.So you can easily differentiate the button inside your Bill method by sender.tag.You already set the tag in cellForRowAt.

    0 讨论(0)
  • 2021-02-11 09:01

    Create IBAction for you button in the custom cell class then declare a block that will be called on button click.

    var ButtonHandler:(()-> Void)!
    
        @IBAction func ButtonClicked(_ sender: Any) {
                self.ButtonHandler()
            }
    

    Then in your tableview class inside the cell for row method,

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = playlistTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
            cell.ButtonHandler = {()-> Void in
                // your code 
            }
            return cell
    }
    
    0 讨论(0)
提交回复
热议问题