我已经制作了一个表格和内容的一些值,我有一个像
这样的按钮cell.textLabel.textColor = [UIColor whiteColor];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:@"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:@selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
我想点击该按钮并从该特定索引中获取值
像
阿里(这里按钮)--------索引0 Sajid(这里按钮)--------索引1 罗伯特(按钮在这里)--------索引2 RaM(此处按钮)--------索引3
Theser是表格的单元格,现在如何点击该按钮获取索引?
如果你没有得到我的问题,你可以再问我一次......
答案 0 :(得分:2)
我找到了答案,
只需在按钮目标方法中编写以下编码,然后使用clickButtonPath作为索引路径。 它工作得很好,只是
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
答案 1 :(得分:0)
btn.tag = indexPath.row;
然后在(void)editTable:(id)sender
:
int row = (UIButton *)btn.tag;
答案 2 :(得分:0)
- (IBAction)editTable:(UIButton *)sender {
UIView *contentView = [sender superView];
UITableViewCell *cell = [contentView superView];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
但首先你应该确保正确地重复使用你的细胞。通过这样更改您的代码:
cell = ... dequeue cell ...
if (cell == nil) {
cell = ... create new cell ...
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = 194;
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:@"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:@selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
}
// if you need to change something for this button you can get it like this:
//UIButton *btn= [cell.contentView viewWithTag:194];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;