简体   繁体   中英

IOS handle event click button in tableView cell

In my cell have 1 button.

I want to change button title when I click.

Exp: default title: Play

When I click button the first it will handle event1, and change title to Stop.

When I click the second, it will change title to Play and handle event2.

...

Understand me? And remember that: button in tableView Cell.

Please help me!

Here is Objective-C version:

- (void) playPauseAction:(id)sender {

    UIButton *button = (UIButton *)sender;

    if ([button.currentTitle isEqualToString:@"Play"]) {
        [button setTitle:@"Pause" forState:UIControlStateNormal];
        // Playing code
    } else {
        [button setTitle:@"Play" forState:UIControlStateNormal];
        // Pausing code
    }
}

Try this

In your cellForRowAtIndexPath

   UIButton *button = [[UIButton alloc]initWithFrame:Your Frame];
   [button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
   Button.tag = indexPath.row;
    [cell.contentView addSubview:Button];

Then write ButtonClicked method

-(void)ButtonClicked:(UIButton*)button{
      if (button.tag == 0)
      {
             //Here button at 0th row is selected.
            //Write your code here
      }
}

Hope it helps.

1) In your cellForRowAtIndexPath: method, assign button tag as index:

cell.yourbutton.tag = indexPath.row;

2) Add target and action for your button as below:

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) Code actions based on index as below in ViewControler:

-(void)yourButtonClicked:(UIButton*)sender
{
     Bool cicked=1;
     if(clicked)
     {
       [cell.yourbutton setTitle: @"myTitle" forState:@""]; 
     }

}
you can try these way......


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
            tags = (int)indexPath.row;
            [tblname reloadData];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier"
    ............
    ............
    ............
    if (indexpath.row == tags){
       [cell.btnName setTitle:@"Newtitle" forState:UIControlStateSelected];
}else{
          [cell.btnName setTitle:@"OldTitle" forState:UIControlStateNoramal];
      }

}

1.Add TargetAction to the button as

[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

2.Just follow action method like this

-(void)buttonClicked:(UIButton*)btn
    {
        if ([btn.titleLabel.text isEqualToString:@"Play"]) {
            [btn setTitle:@"Stop" forState:UIControlStateNormal];
        }else{
             [btn setTitle:@"Play" forState:UIControlStateNormal];
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM