我正在显示自定义单元格中的表格 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/