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)
}
If you need to pass string just use accessibilityHint of UIButton.
Tag can only store Int so if anyone wants to pass string data can use this. However, this not a proper way of passing data!
example:
I think we should architect the code well. The hacks will just make your code hard to understand and buggy. Here is some suggestion.
Make a custom button class
Now add target to your button
Now simply get your values in the method call
May be you can do something like this
Create tag value according to you'r requirement and maintaint it's integrity too.
Easy!
1: Subclass
UIButton
:2: Set your value:
3: Retrieve your value:
You can use the
tag
property on the UIButtons'stitleLabel
andUIImageView
to add additional parameters