【问题标题】:Button index in table,iPhone [duplicate]表中的按钮索引,iPhone [重复]
【发布时间】:2012-06-22 08:06:52
【问题描述】:
我在UITableView 的每一行都有UIButtons。
如何检测按钮索引(就像我们可以通过didSelectRowIndexAtIndexPath 方法检测表中的行索引一样)?
例如:我有一个 UITableView 和自定义 UITableViewCells。
在 10 行中,有 6 行有 UIButtons。
点击按钮时,我将用户带到下一个视图(详细视图)。
但我无法获得UIButton(行)索引。因为,我不是通过didSelectRowIndexAtIndexPath方法让用户点击按钮,所以我无法获得行索引。
我怎样才能得到它?
建议。
谢谢
【问题讨论】:
标签:
iphone
ipad
tableview
【解决方案1】:
这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//other code
// your button reference here
// if custom cell then use cell.yourbutton
[button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
//other code
}
现在按钮选择器将如下所示:
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int row = button.tag;
}