Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send multiple tags with UIButton in UITableView

Im suffering from sending two tags to a function to show UIActivityViewController and share the cell data, well before i used to send one value at a time and within a single UIButton:

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

But now I've implemented sections in my UITableView so my array is like :

Array[indexPath.section][indexPath.row]

One of them (section or row) is not enough at a time, i need to send both to my function ? how can i do that ?

func showAlert(sender:AnyObject){
    // i want to use it like :
    Array[sender.sectionvalue][sender.rowvalue]
}
like image 999
AaoIi Avatar asked May 19 '26 06:05

AaoIi


1 Answers

You could subClass UIButton and add properties for the data you need eg

class MyButton : UIButton {

var row : Int?
var section : Int?

}

you can then set those properties, and in your showAlert function you can get them back and use:

 func showAlert(sender:AnyObject){

    let theButton = sender as! MyButton

    let section = theButton.section
    let row = theButton.row

    }

Edit: Added where to set the button as per comment requested:

In your StoryBoard, make sure that your button is of type MyButton (and not UIButton anymore).

and then where you used to set the tag, don't use the tag but set the properties. So replace this code :

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

with:

 cell.sharefb.row = indexPath.row
 cell.sharefb.section = indexPath.section
 cell.sharefb.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)
like image 117
Glenn Avatar answered May 21 '26 18:05

Glenn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!