![](/img/trans.png)
[英]Two Button click event in single cell in TableView in iOS(Xamarin)
[英]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.