【问题标题】:UIButton created dynamically in UITableViewCell at Row Editing time not responding to touch event在行编辑时在 UITableViewCell 中动态创建的 UIButton 不响应触摸事件
【发布时间】:2012-08-01 07:00:24
【问题描述】:

我正在显示自定义单元格中的表格视图,并且工作正常。 但问题是在 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];

}

【问题讨论】:

  • 你的这个方法永远不会被调用,这就是为什么永远不会添加按钮。 - (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 最好在 cellForRowAtIndexPath 中添加按钮
  • 添加了亲爱的按钮,当单元格滑动问题是它从不接受触摸而删除按钮执行时,它与删除按钮一起出现
  • 好的,让我检查一下..我正在实施所有这些。
  • 我试过了..但我想在删除模式下只有删除按钮可以工作..所以这是不可能的..
  • @iPhoneDeveloper 非常感谢您的努力

标签: iphone uitableview


【解决方案1】:

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

我的两个建议

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

第二个建议是最坏的情况

2:尝试使用 GustureRecognizers

【讨论】:

  • 先生。 Ramshad 我添加了你的代码 [cell.contentView bringSubviewToFront:btnAdd];就在将按钮添加到 willBeginEditingRowAtIndexPath 中的子视图下方,它不起作用。
  • 请告诉我您如何使用 btnAdd 和我的代码调用方法?
  • GustureRecognizers 是你的好主意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2016-01-06
  • 2016-12-14
  • 2013-09-26
  • 2017-05-22
  • 1970-01-01
相关资源
最近更新 更多