0

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!

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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
    }
}

Comments

0

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:@""]; 
     }

}

2 Comments

No! Change title button and change handle event click button.
In addition to the above solution, you can configure the button for label text for different button states and on button click selector, just toggle the state of the button.
0
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];
      }

}

Comments

0

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];
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.