2012-01-08 11 views
0

私はテーブルビューを持っています。そして、IMは、各セルに2つのボタンが追加:表が「編集」モードのときにプログラムでUIButtonを追加するとUITableViewのバグが発生する

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     newBtn = [[UIButton alloc]init]; 
     newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [newBtn setFrame:CGRectMake(250,10,25,55)]; 
     [newBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [newBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [newBtn setEnabled:YES]; 
     newBtn.hidden = YES; 
     [cell addSubview:newBtn]; 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(280,10,25,55)]; 
     [subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"-" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     subBtn.hidden = YES; 
     [cell addSubview:subBtn]; 

    } 
return cell; 
} 

と私は最初に隠されたボタンを持つようにしたい、そして、私は、これらのボタンを表示します。テーブルが「編集」モードを離れると、ボタンは消えます。

これを行うには、セルボタンの1つを取得できます。

- (IBAction)editButton:(id)sender 
{ 
    if (self.editing) 
    { 
     [self setEditing:NO animated:YES]; 
     [self.myTableView setEditing:NO animated:YES]; 
     EditButton.title = @"Edit"; 
     subBtn.hidden = YES; 
     newBtn.hidden = YES; 
    } 
    else 
    { 
     [self setEditing:YES animated:YES]; 
     [self.myTableView setEditing:YES animated:YES]; 
     EditButton.title = @"Done"; 
     subBtn.hidden = NO; 
     newBtn.hidden = NO; 
    } 
} 

しかし、問題は次のとおりです。これを行うと、最後のセルだけがボタンを取得します。彼らは私が欲しいときに正確に現れ、消えますが、最後の細胞だけです!他のセルにはボタンがありません。誰かが私を助けてくれますか?本当にありがとう!

答えて

1

この操作を行う方法は、最後のセルのボタンを指すsubBtnnewBtnです。より良いアプローチは、UITableViewCellをサブクラス化し、ボタンのインスタンス変数を作ることです。その後、- (void)setEditing:(BOOL)editing animated:(BOOL)animatedを上書きしてボタンを非表示にします。

+0

少し説明してください。イムノブ!笑「サブコール」ってどういう意味ですか?あまりに面倒でもない場合は、私はそれを勉強して自分のプログラムで使用できるようなサンプルコードを提供してください。助けてくれてありがとう! – iProRage

+0

また、私のボタンは最後のセルのボタンにどのように「ポイント」しますか?私はどのコードがそれをしているのかわからない!再度、感謝します! :D – iProRage

+0

私は 'サブクラス'を意味しました。あなたが行うたびに見てください "subBtn = [[UIButton alloc] init];"あなたのポインタは明らかにオーバーライドされているので、最後には最後のセルのボタンを指しています。これをよりよく説明する方法を知らないでください。 –

1

「編集ボタン」方法はどのように起動していますか? didSelectRowAtIndexPathを使用している場合は、選択した行だけがボタンを表示します。目に見えるセルに対してindexpath.rowを繰り返し、各行の表示を解除することができます。

+0

私はnav barのボタンからこのメソッドを呼び出すだけで、助けてくれてありがとう! – iProRage

関連する問題