在我的表格视图中,每个单元格中都有一个按钮
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(281,0, 20, 50);
UIImage *editButtonImage = [UIImage imageNamed:@"edit.png"];
[editButton setImage:editButtonImage forState:UIControlStateNormal];
[editButton setBackgroundColor:[UIColor clearColor]];
[editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:editButton];
按钮动作是这个
-(IBAction)editButtonAction:(id)sender{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
NSIndexPath *indexPath =[self.table indexPathForCell:cell];
int row = indexPath.row;
NSLog(@"Selected row is: %d",row);
}
当我在表格视图中单击按钮时,所选行始终为0
有什么想法吗?和任何代码
最佳答案
替代解决方案:
.
.
editButton.tag = indexPath.row;
[editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
.
.
-(IBAction)editButtonAction:(id)sender{
UIButton *btn = (UIButton*) sender;
NSLog(@"Selected row is: %d",btn.tag);
}