2

カスタムセルからテーブルビューを表示しており、正常に動作しています。しかし問題は、tableCell 内で作成された UiButton がセレクターに応答しないことです。

行をスワイプして編集します 次に、 titleForDeleteConfirmationButtonForRowAtIndexPathに UIButton を作成します。これは削除ボタンと同時に表示され、削除ボタンをタッチするとcommitEditingStyleに応答します。その後、削除ボタンと追加ボタンが消えます。再び行をスワイプしてから、追加および削除ボタンが表示されます追加ボタンをクリックすると、常にcanEditRowAtIndexPathに応答してからdidEndEditingRowAtIndexPathに応答します。

btnMethodを呼び出さない理由がわかりません。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];

}
4

1 に答える 1

2

サンプルアプリケーションを作成しました。上記のコードを使用すると、うまくいきました。

私からの2つの提案

1: [cell.contentView BringSubviewToFront:btnAdd] を使用します。

2番目の提案は最悪のケースになります

2: GustureRecognizersで試す

于 2012-08-01T07:20:32.497 に答える