2015-07-28 83 views
0

在我的單元格中有1個按鈕。IOS處理事件單擊按鈕在tableView單元格

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

經驗:默認標題:玩

當我點擊按鈕,首先它會處理事件1,並更改標題停止。

當我點擊第二個,它會改變標題爲播放和處理事件2。

...

瞭解我嗎?請記住:tableView Cell中的按鈕。

請幫幫我!

回答

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

} 
+0

不!更改標題按鈕並更改句柄事件點擊按鈕。 – user5068763

+0

瞭解我嗎? – user5068763

+0

立即檢查...... – vijeesh

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

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

1.增加TargetAction到按鈕作爲

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

2.Just遵循這樣

 

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

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

希望它能幫助。