簡體   English   中英

iOS在TableView單元中處理事件單擊按鈕

[英]IOS handle event click button in tableView cell

在我的單元格中有1個按鈕。

我想在單擊時更改按鈕標題。

Exp:默認標題:播放

當我單擊第一個按鈕時,它將處理event1,並將標題更改為Stop。

當我單擊第二個按鈕時,它將標題更改為“播放”並處理event2。

...

明白我? 並記住:tableView Cell中的按鈕。

請幫我!

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

嘗試這個

在您的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
      }
}

希望能幫助到你。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM