2

我正在从自定义单元格显示表格视图,并且工作正常。但问题是在 tableCell 中创建的 UiButton 没有响应选择器。

我滑动行进行编辑然后我在 titleForDeleteConfirmationButtonForRowAtIndexPath创建 UIButton 。当删除按钮,触摸删除按钮响应commitEditingStyle:然后删除按钮和添加按钮Disappers时,这会同时出现。我再次滑动行然后出现添加和删除按钮如果我单击添加按钮它总是响应canEditRowAtIndexPath然后didEndEditingRowAtIndexPath

我不知道为什么它不调用btnMethod。请让我知道如何使 btnMethod 可以从 uibutton touchDown 事件中调用。

这是我的代码:

- (void)tableView:(UITableView *)tableView1 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) 
{
    UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];

    UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];


    btn.hidden=YES;

    [btn removeFromSuperview];
}    
}


 - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btnAdd=[UIButton buttonWithType:UIButtonTypeCustom];
btnAdd.frame=CGRectMake(8, 6, 80, 30);
[btnAdd setTitle:@"Add" forState:UIControlStateNormal];
btnAdd.backgroundColor=[UIColor magentaColor];
btnAdd.titleLabel.textAlignment=UITextAlignmentCenter;
btnAdd.titleLabel.textColor=[UIColor whiteColor];
btnAdd.layer.masksToBounds=YES;
btnAdd.layer.cornerRadius=6.0;
btnAdd.layer.borderWidth=2.0;
btnAdd.layer.borderColor=[UIColor blackColor].CGColor;
btnAdd.tag=indexPath.row+433;
[btnAdd addTarget:self action:@selector(btnMethod:) forControlEvents:UIControlEventTouchUpInside];
btnAdd.userInteractionEnabled=YES;
[cell11.contentView addSubview:btnAdd];
return @"Drop Me";
}
 - (BOOL)tableView:(UITableView *)tableView1 canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (void)tableView:(UITableView*)tableView1 didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];
btn.hidden=YES;
[btn removeFromSuperview];

}
-(void)btnMethod:(id)sender
{
UITableViewCell *cell=(UITableViewCell*)[sender superview];

}
4

1 回答 1

2

我已经创建了示例应用程序。使用您上面的代码对我来说效果很好。

我这边的两个建议

1:使用[cell.contentView带来SubviewToFront:btnAdd];

第二个建议将是最坏的情况

2:尝试使用GustureRecognizers

于 2012-08-01T07:20:32.497 回答