2011-12-26 11 views
0

UIButtonをテーブルセルに動的に追加する必要があります - 以下の要件を参照してください。ユーザがセルを選択した場合UITableViewCellにUIButtonを動的に追加するにはどうすればいいですか?

、私は単に

-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath { 
     selIndPath = [indexPath retain]; 
     [self.tableView beginUpdates]; 
     [self.tableView endUpdates]; 
} 
にそのテーブルのセルの高さを調整してい上記の方法は、その選択された行の高さを調整するためのtableViewを強制します。しかし、上記の方法でUIButtonを追加すると、ボタンは表示されません。

私は[self.tableView reloadData]に興味がありません。

大変助かりました。

** * ** EDITED* ** * ***

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (selIndPath && selIndPath == indexPath) { 
     //UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
//  UIButton* btn = (UIButton*)[[cell contentView] viewWithTag:1234]; 
//  btn.frame = CGRectMake(40, 60, 60, 24); 
//  [cell setNeedsDisplay]; 
     return 90; 
    } 
    return 64; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     [[cell contentView] setBackgroundColor:[UIColor clearColor]]; 
     [[cell backgroundView] setBackgroundColor:[UIColor clearColor]]; 
    } 

    // THIS LINES OF CODE WORKS ONLY ON reloadData 
    if (selIndPath && selIndPath == indexPath) { 
     UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.frame = CGRectMake(40, 60, 60, 24); 
     btn.tag = 1234; 
     [btn setTitle:@"Hi" forState:UIControlStateNormal]; 
     [cell.contentView addSubview:btn]; 
    } 

    // Configure the cell. 
    cell.textLabel.text = @"Loading.."; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    selIndPath = [indexPath retain]; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    //[self.tableView reloadData]; 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
} 
+0

を追加するには、あなたがUIButtonを追加するために使用していたコードを表示することができますか? – Louis

+0

返信いただきありがとうございます、私は私の質問にいくつかの追加のコードを追加しました。それはあなたがそれを理解するのに役立つかもしれません。 – prathumca

答えて

2

あなたはそれを追加した場合、ボタンの追加は動作しません。セルにcellForRowAtIndexPath:。何をする必要があるボタンと何を追加するか、方法もちろん

-(void) addButtonToCell 
{ 
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn.frame = CGRectMake(40, 60, 60, 24); 
    btn.tag = 1234; 
    [btn setTitle:@"Hi" forState:UIControlStateNormal]; 
    [self.contentView addSubview:btn]; 

} 

を作成し、カスタムセルで

(how- Changing the position of custom UIButton in custom UITableViewCellにこの質問の回答を参照してください)カスタムUITableViewCellを作成することですこのメソッドに渡す引数はあなたのものですが、要点を得ることができます。 cellForRowAtIndexPath

:、単に

if (selIndPath && selIndPath == indexPath) { 
[cell addButtonToCell]; 
} 
+0

返事をありがとう。これは、[tableView reloadData]を呼び出した場合にのみ機能します。前述のように、私はreloadDataには興味がありません。私のテーブルは非常に大きなデータを持っているので、行の高さの変化によるアニメーションの影響はありません。 – prathumca

+0

@prathumca:このメソッドを使用する方法 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)アニメーション テーブルビュー。それはあなたがしたい行だけをリロードします。 – MadhavanRP

+0

私はそれを試してみましたが、現在は運がありません。アニメーションはさほど滑らかではなく、ボタンは正しく表示/整列されません。どのような助け本当に感謝します。 – prathumca

関連する問題