【问题标题】:IOS handle event click button in tableView cellIOS处理tableView单元格中的事件点击按钮
【发布时间】:2015-07-28 10:45:02
【问题描述】:

在我的单元格中有 1 个按钮。

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

Exp:默认标题:播放

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

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

...

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

请帮帮我!

【问题讨论】:

    标签: ios objective-c uibutton


    【解决方案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】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 不!更改标题按钮和更改句柄事件单击按钮。
        • 除上述方案外,您还可以为不同的按钮状态和按钮单击选择器配置标签文本按钮,只需切换按钮的状态。
        【解决方案4】:
        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];
              }
        
        }
        

        【讨论】:

          【解决方案5】:

          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]; }别的{ [btn setTitle:@"Play" forState:UIControlStateNormal]; } }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-09-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-15
            相关资源
            最近更新 更多