(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:@"OK" forState:UIControlStateNormal];
[btn setTitle:@"OK" forState:UIControlStateSelected];
[btn addTarget:self action:@selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
return cell;
}
但是如果用户选择特定行上的按钮,我可以通过onClickRename更改该图像...但是我可以通过
来获取行的图像答案 0 :(得分:2)
[btn setTag:[indexPath] row]
在您的单元格设置中,然后
- (void) onClickRename:(id)sender {
int row = sender.tag;
}
你知道哪个表格行被击中了。
答案 1 :(得分:0)
Apple在其Accessory示例代码中使用以下技术:
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
// do what you want with the item at indexPath
}
}
答案 2 :(得分:0)
在Swift中,您可以为每个按钮设置标签
此外,将每个索引值设置为其标签,我们将知道在哪一行单击了哪个按钮。
在cellForAtIndexPath中设置标签
cell.button.tag = indexPath.row
in button Action
@IBAction func anAction(_sender : AnyObject){
let tappedButton = sender.tag // gives the row and button
}