【发布时间】:2015-07-28 10:45:02
【问题描述】:
在我的单元格中有 1 个按钮。
我想在单击时更改按钮标题。
Exp:默认标题:播放
当我点击第一个按钮时,它将处理 event1,并将标题更改为停止。
当我点击第二个时,它会将标题更改为播放并处理 event2。
...
了解我吗?请记住:tableView Cell 中的按钮。
请帮帮我!
【问题讨论】:
标签: ios objective-c uibutton
在我的单元格中有 1 个按钮。
我想在单击时更改按钮标题。
Exp:默认标题:播放
当我点击第一个按钮时,它将处理 event1,并将标题更改为停止。
当我点击第二个时,它会将标题更改为播放并处理 event2。
...
了解我吗?请记住:tableView Cell 中的按钮。
请帮帮我!
【问题讨论】:
标签: ios objective-c uibutton
试试这个
在你的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
}
}
希望对你有帮助。
【讨论】:
这里是 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) 在你的 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]; }别的{ [btn setTitle:@"Play" forState:UIControlStateNormal]; } }【讨论】: