2012-08-01 43 views
2

我正在显示自定义单元格的tableview和它的工作正常。 但问题是,在tableCell内部创建的UiButton没有响应选择器。UITableViewCell在行中动态创建的UIButton编辑时没有响应触摸事件

我滑动编辑行然后我创建UIButton在titleForDeleteConfirmationButtonForRowAtIndexPath。 出现同时删除按钮时,触摸删除按钮响应commitEditingStyle:然后删除按钮和添加按钮消除器。我再次刷卡行再添加&删除按钮显示如果我点击添加按钮它总是响应canEditRowAtIndexPath然后didEndEditingRowAtIndexPath

我不明白为什么它不打电话btn方法。请让我知道如何使它可以从uibutton touchDown事件调用btnMethod。

这里是我的代码:

- (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]; 

} 
+0

试试你的这种方法不会被调用,这就是为什么从不添加的按钮。 - (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath最好在cellForRowAtIndexPath中添加按钮 – 2012-08-01 11:02:15

+0

亲爱的按钮已添加,并且在单元格滑动问题是其永不接受删除按钮做 – 2012-08-01 11:09:33

+0

好吧,让我检查..我正在执行所有这一切。 – 2012-08-01 11:25:44

回答

2

我创建示例应用程序。它对我使用上面的代码很好。从我的侧

两个建议

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

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

2:GustureRecognizers

+0

先生Ramshad我添加了您的代码[cell.contentView bringSubviewToFront:btnAdd ]。只是在将按钮添加到willBeginEditingRowAtIndexPath中的子视图时不起作用。 – 2012-08-01 07:31:20

+0

请让我知道你如何调用一个使用btnAdd方法的方法? – 2012-08-01 07:45:00

+0

请问我可以寄给我的修改后的代码吗? – 2012-08-01 09:41:39