简体   繁体   English

iOS在TableView单元中处理事件单击按钮

[英]IOS handle event click button in tableView cell

In my cell have 1 button. 在我的单元格中有1个按钮。

I want to change button title when I click. 我想在单击时更改按钮标题。

Exp: default title: Play Exp:默认标题:播放

When I click button the first it will handle event1, and change title to Stop. 当我单击第一个按钮时,它将处理event1,并将标题更改为Stop。

When I click the second, it will change title to Play and handle event2. 当我单击第二个按钮时,它将标题更改为“播放”并处理event2。

... ...

Understand me? 明白我? And remember that: button in tableView Cell. 并记住:tableView Cell中的按钮。

Please help me! 请帮我!

Here is Objective-C version: 这是Objective-C版本:

- (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 在您的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 然后写ButtonClicked方法

-(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: 1)在您的cellForRowAtIndexPath:方法中,将按钮标签分配为索引:

cell.yourbutton.tag = indexPath.row;

2) Add target and action for your button as below: 2)为按钮添加目标和操作,如下所示:

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

3) Code actions based on index as below in ViewControler: 3)在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 1.将TargetAction添加到按钮

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

2.Just follow action method like this 2.只需遵循这样的动作方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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