8

Pass several parameters to AddTarget

 2 years ago
source link: https://www.codesd.com/item/pass-several-parameters-to-addtarget.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pass several parameters to AddTarget

advertisements

In my UITableViewCell I have a button. And I want to add action to it by passing multiple parameters in cellForRowAtIndexPath method.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:
        indexPath) as! CartTableViewCell
    cell.buyButton.addTarget(self, action: self.buyButton(indexPath, 2, 3 ,4 , 5, 6), forControlEvents: .TouchUpInside)
}


May be you can do something like this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:indexPath) as! CartTableViewCell
    cell.buyButton.tag = (indexPath.section*100)+indexPath.row
    cell.buyButton.addTarget(self, action: "btnBuy_Click:", forControlEvents: .TouchUpInside)
}

func btnBuy_Click(sender: UIButton) {
        //Perform actions here
     let section = sender.tag / 100
     let row = sender.tag % 100
     let indexPath = NSIndexPath(forRow: row, inSection: section)
     self.buyButton(indexPath, 2, 3 ,4 , 5, 6)
}

Create tag value according to you'r requirement and maintaint it's integrity too.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK