2009-05-12 13 views

答えて

8

あなたは本当にただのセルのcontentViewにボタンを追加し、サブクラス化なしカスタムボタンを追加したい場合は、次の

[cell.contentView addSubview:customButton]; 

あなたは、すべてのボタンの特性を設定することができます:フレーム、ターゲット、セレクタを、 etc ...広告はそれをセルに追加するために上記の呼び出しを使用しました。

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
customButton.frame=//whatever 
[customButton setImage:anImage forState:UIControlStateNormal]; 
[customButton setImage:anotherImage forState:UIControlStateHighlighted]; 
[customButton addTarget:self action:@selector(delete) forControlEvents: UIControlEventTouchUpInside]; 
//yadda, yadda, ..... 

あなたは同様にそれをタグ付けすることができます

customButton.tag = 99999; 

だから、あなたが後でそれを見つけることができます。

UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999]; 
多分、セルの選択に、ボタンを追加したときに決定する必要があります

、多分編集モードで...あなたの選択したデリゲートメソッドにコードを入れてください。

1

ボタンの唯一の目的が削除を提供することである場合、には- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPathというメソッドがあります。

- (BOOL)tableView:(UITableView *)tableView 
canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

をそして実装:そうのようにそれを実装あなたのヘッダーに

MyClass : UITableViewController <UITableViewDataSource> 

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Database removal code goes here... 
} 

を聞かせて、これらのメソッドを使用するには、あなたのUITableViewControllerのような何かを行うことによってUITableViewDataSourceプロトコルを実装しますviewControllerのデータソースをselfに設定することを忘れないでください。

+0

ボタンについてカスタムボタンをクリックすると、このようなことをすべてやりたいのですが、インターフェイスビルダーとカスタムセルを使用せずにボタンを作成する必要があります。 –

+0

canEditRowAtIndexPathメソッドは、編集モードのときに自動的に各行に削除ボタンを追加します。それを試してください: – tmadsen

+0

しかし、私はカスタムボタンでそれをやりたいです。 –

関連する問題