我正在将自定义按钮添加到我的UITableViewCell
。在该按钮的操作中我想打电话showAlert:
函数并希望在方法中传递单元格标签。
我如何在其中传递参数showAlert
方法:action:@selector(showAlert:)
?
如果您在 Tableviewcell 中使用 Button,那么您必须向每个单元格的按钮添加标签值,并使用 id 作为参数设置方法 addTarget。
示例代码:
您必须输入以下代码cellForRowAtIndexPath
method.
{
// Set tag to each button
cell.btn1.tag = indexPath.row;
[cell.btn1 setTitle:@"Select" forState:UIControlStateNormal]; // Set title
// Add Target with passing id like this
[cell.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)btnClick:(id)sender
{
UIButton* btn = (UIButton *) sender;
// here btn is the selected button...
NSLog(@"Button %d is selected",btn.tag);
// Show appropriate alert by tag values
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)