In Swift, how can I get the row of the cell in which a button was pressed? -


i've tried indexpathforrowatpoint solution , worked , don't know broke it. have advice common mistakes might have made? thanks.

    let pointintable = sender.convertpoint(sender.bounds.origin, toview: self.tableview)     let index = self.tableview.indexpathforrowatpoint(pointintable)?.row     let prod_id = list[index].getprodid() 

i create custom cell have closure callback, triggered button

import uikit  class buttoncell: uitableviewcell {      @iboutlet weak var button: uibutton! {         didset{             button.addtarget(self, action: #selector(buttoncell.buttontapped(_:)), forcontrolevents: .touchupinside)         }     }      var buttonwastapped: ((cell: buttoncell) -> void)?      override func setselected(selected: bool, animated: bool) {         super.setselected(selected, animated: animated)     }      func buttontapped(sender:uibutton) {         buttonwastapped?(cell: self)     } } 

now in datasource (note: here implemented in view controller, better separate datasource object) set callback identify cell, , index path

import uikit  class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {      @iboutlet weak var tableview: uitableview!     func numberofsectionsintableview(tableview: uitableview) -> int {         return 1     }      func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         return 30     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! buttoncell         cell.buttonwastapped = {             cell in             let idxpath = tableview.indexpathforcell(cell)             let alert = uialertcontroller(title: "tapped", message: "cell @ indexpath: \(idxpath)", preferredstyle: .alert)             alert.addaction(uialertaction(title: "ok", style: .default, handler: nil))             self.presentviewcontroller(alert, animated: true, completion: nil)         }         return cell     } } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -