How to Set Action for Uibutton in Uitableviewcell

How to set Action for UIButton in UITableViewCell

This piece of code will help you

static NSString *CellIdentifier = @"cellTimer";
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
cell = [nib objectAtIndex:0];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=indexPath.row;
[button addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"cellButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0);
[cell.contentView addSubview:button];
}

return cell;
}

-(void)aMethod:(UIButton*)sender
{
NSLog(@"I Clicked a button %d",sender.tag);
}

Hope this helps!!!

UIButton action in table view cell

Swift 4 & Swift 5:

You need to add target for that button.

myButton.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

And of course you need to set tag of that button since you are using it.

myButton.tag = indexPath.row

You can achieve this by subclassing UITableViewCell. Use it in interface builder, drop a button on that cell, connect it via outlet and there you go.

To get the tag in the connected function:

@objc func connected(sender: UIButton){
let buttonTag = sender.tag
}

How to set IBAction for a UIButton in a UITableViewCell for a different ViewController?

add this in the cellForRowAtIndexPath

cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);

And anywhere in the same UIVeiwController define the function as below

func tappedButton(sender : UIButton){
// Your code here
}

UITableViewCell Buttons with action

I was resolving this using a cell delegate method within UITableViewCell's subclass.

Quick overview:

1) Create a protocol

protocol YourCellDelegate : class {
func didPressButton(_ tag: Int)
}

2) Subclass your UITableViewCell (if you haven't done so):

class YourCell : UITableViewCell
{
var cellDelegate: YourCellDelegate?
@IBOutlet weak var btn: UIButton!
// connect the button from your cell with this method
@IBAction func buttonPressed(_ sender: UIButton) {
cellDelegate?.didPressButton(sender.tag)
}
...
}

3) Let your view controller conform to YourCellDelegate protocol that was implemented above.

class YourViewController: ..., YourCellDelegate {  ... }

4) Set a delegate, after the cell has been defined (for reusing).

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCell
cell.cellDelegate = self
cell.btn.tag = indexPath.row

5) In the same controller (where is your implemented UITableView delegate/datasource), put a method from YourCellDelegate protocol.

func didPressButton(_ tag: Int) {
print("I have pressed a button with a tag: \(tag)")
}

Now, your solution is not tag / number dependent. You can add as many buttons as you want, so you are ready to get response via delegate regardless how many buttons you want to install.

This protocol-delegate solution is preferred in iOS logic and it can be used for other elements in table cell, like UISwitch, UIStepper, and so on.

Swift - Adding target to UIButton in UITableView not working

It's a matter of hierarchy, you need to add the buttons to the contentView. If you just add a subview to the view, it goes behind the contentView of the UITableViewCell.

Visually, where the selected one is the contentView of the cell, you can see that the actual button is behind it:
enter image description here

So contentView.addSubview(editButton) and contentView.addSubview(deleteButton) does the job.

set action for UIButton in customized UITableViewCell which has many other views

First add one button in the contentView of UITableViewCell in the xib file and create the outlet in customCell. After that change your code of cellForRowAtIndex like this.

[cell.btn addTarget:self action:@selector(btnTap:) forControlEvents:UIControlEventTouchUpInside];

After that create this btnTap method

- (void)btnTap:(UIButton*)sender {
CustomCell *cell = (CustomCell*) [[sender superview] superview]; //If you have button inside another view you need to add superView on your hierarchy basis.
NSIndexPath *indexPath = [self.tblFile indexPathForCell:cell];
NSLog(@"Row you want to delete - %d",indexPath.row);
}

Hope this will help you.

Uibuttons inside a custom uitableviewcell

In your cellForRow function, add button listener in this way

cell.plusBtn.addTarget(self, action: "plusBtnClicked:", for: .touchUpInside)
cell.minusBtn.addTarget(self, action: "minusBtnClicked:", for: .touchUpInside)

Then receive the call by

func plusBtnClicked(_ sender: AnyObject?) {
score += 1
self.tableView.reloadData()
}

func minusBtnClicked(_ sender: AnyObject?) {
score -= 1
self.tableView.reloadData()
}

UIButton not responding used in a custom UITableViewCell

I would have userInteractionEnabled set to true on the table view cell as well. I would prevent taps using the UITableView allowsSelection to false

Also remember to remove the target and action in tableView:cellForRowAtIndexPath: since the cells are recycled, the button might already have the target and action, it might add a second.



Related Topics



Leave a reply



Submit