IOS处理tableView单元格中的事件单击按钮

时间:2015-07-28 10:45:03

标签: ios objective-c uibutton

在我的手机中有1个按钮。

我想在点击时更改按钮标题。

Exp:默认标题:播放

当我点击第一个按钮时,它将处理event1,并将标题更改为停止。

当我点击第二个时,它会将标题更改为Play并处理event2。

...

了解我?请记住:tableView Cell中的按钮。

请帮助我!

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

试试这个

cellForRowAtIndexPath

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

然后编写ButtonClicked方法

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

希望它有所帮助。

答案 2 :(得分:0)

1)在您的cellForRowAtIndexPath:方法中,将按钮标记指定为索引:

cell.yourbutton.tag = indexPath.row;

2)为按钮添加目标和操作,如下所示:

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

3)ViewControler中基于索引的代码操作:

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

}

答案 3 :(得分: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];
      }

}

答案 4 :(得分:0)

1.将TargetAction添加到按钮

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

2.只需遵循这样的行动方法


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