iphone - 在行编辑时在 UITableViewCell 中动态创建的 UIButton 不响应触摸事件

标签 iphone uitableview

我正在显示自定义单元格中的表格 View ,并且它工作正常。 但问题是在 tableCell 内创建的 UiButton 不响应选择器。

我滑动行进行编辑然后我在 titleForDeleteConfirmationButtonForRowAtIndexPath 中创建 UIButton 。 当“删除”按钮出现时,触摸“删除”按钮会响应commitEditingStyle:,然后“删除”按钮和“添加”按钮消失。我再次滑动行,然后出现“添加和删除”按钮,如果我单击“添加”按钮,它总是响应 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];

}

最佳答案

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

我的两个建议

1:使用[cell.contentView BringSubviewToFront:btnAdd];

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

2:尝试使用GustureRecognizers

关于iphone - 在行编辑时在 UITableViewCell 中动态创建的 UIButton 不响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11754100/

相关文章:

iphone - 在应用程序内购买没有有效的产品 ID

iphone - iPhone 中的小区广播

objective-c - 填充餐 table 带有firebase中的图像

swift - 如何在 Swift 中使用 Objc 在带有按钮的表格 View 中打开 View Controller ?

swift - UITableViewCell 滚动时继续更改其图像

iphone - 如何从 SQL 数据库获取数据到 Core-Data?

android - 移动设备使用 PGP 会不会太过分了?

iphone - 如何在 Objective C 中将 ISO-8859-1 编码的字符串转换为 UTF-8

ios - 单击时 UITableViewCell 背景颜色变为白色

ios - Swift 2.0 错误从 '[NSIndexPath]?' 向下转换为 '[indexpath]' 仅解包可选