Greg Peckory Greg Peckory - 1 year ago 96
iOS Question

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
, or some alternative??



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.

Answer Source

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


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


cell.plusBut.indexPath = indexPath

Choose whatever suits you.