2009-10-26 19 views
5

テーブルの行を選択するときに、tableviewに新しい行を追加する必要があります。iphoneのTableViewで動的に行を追加する

私は次の方法を使用すると思いますか?

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

新しい行を追加する方法の中でどのような記述をしますか?

事前 Shibinで、私を助けて

感謝を行ってください。

答えて

2

あなたは配列から、あなたのテーブルを充填している場合、あなたは自分の配列に要素を追加し、私は動的に行を追加すると、私はinsertRowsAtIndexPathsを使用[myTable reloadData]

+0

いいえ、配列から新しい行データをロードしていません。必要なのは、テーブルの特定の行を選択するときに新しい行を追加することです。それはただ私の既存のテーブルに追加されます。 – smakstr

+0

Hmm。そしてあなたのテーブルビューのコンテンツのソースは何ですか? – Morion

+1

numberOfRowsInSectionによって返される項目の数を変更し、cellForRowAtIndexPathによって返されるデータを適切に更新するだけで済みます。上記のようにreloadDataを呼び出します。 –

13

を呼び出すことができます。ここでは例の機能が

- (void) allServersFound { 
    // called from delegate when bonjourservices has listings of machines: 
    bonjourMachines = [bonjourService foundServers]; // NSArray of machine names; 

    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

    int i = 0; 
    for (NSArray *count in bonjourMachines) { 
     [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; 
    } 

    [[self tableView] beginUpdates]; 
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationNone]; 
    [[self tableView] endUpdates]; 

    [tempArray release]; 
} 
です
0

ReloadDataがtebleビュー内の行を追加することができますが、行が追加したときに、いくつかのアニメーションをしたい場合、あなたは、これらの行を使用する必要があります...いいと簡単ですが、私の知る限りアニメーションはありませんコード。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
     [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]; 

     [mainArray insertObject:@"new row" atIndex:indexPath.row+1]; 

     [[self tableView] beginUpdates]; 
     [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade]; 
     [[self tableView] endUpdates]; 

} 
関連する問題