iosswiftuitableviewbuttonsender-id

Send Row and Section through Tag in Button Swift


I have this inside cellForRowAtIndexPath

   cell.plusBut.tag = indexPath.row
   cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside)

and this function outside:

func plusHit(sender: UIButton!){
    buildings[sender.tag].something = somethingElse
}

Is it possible to send the indexPath.row and indexPath.section, or some alternative??

Thanks!

EDIT

I approached it like this:

My Custom Button

class MyButton: UIButton{

    var myRow: Int = 0
    var mySection: Int = 0

}

My Custom Cell

class NewsCell: UITableViewCell{

    @IBOutlet weak var greenLike: MyButton!

In CellForRowAtIndexPath

    cell.greenLike.myRow = indexPath.row

I get an error on this line.


Solution

  • You can create a subclass of UIButton and create an extra property section in it. And then you can use that class for cell button. You can do the same for row.

    Here are few possible ways after subclassing UIButton

    cell.plusBut.tag = indexPath.row
    cell.plusBut.section = indexPath.section
    

    Or

    cell.plusBut.row = indexPath.row
    cell.plusBut.section = indexPath.section
    

    Or

    cell.plusBut.indexPath = indexPath
    

    Choose whatever suits you.