2012-03-21 17 views
0

テーブルビューの最後の行の前に同時に多くの行を挿入したいが、最後の行の前に行を追加し、最後に2行を追加する。どのようにそれを把握するには?お手伝いをしてください、ありがとうございます。 !同時にテーブルビューにさらに多くの行を挿入するには?

- (void)morePicture:(id)sender{ 
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; 
    for (int i=0; i<3; i++) { 
     NSString *s = [[NSString alloc] initWithFormat:@"%d",i]; 
     [photos addObject:s]; 
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0]; 
     [indexPaths addObject:indexpath]; 
    } 

    [table beginUpdates]; 
    [table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
    [table endUpdates]; 

    [table reloadData]; 
} 

enter image description here

答えて

3
- (void)morePicture:(id)sender { 
    // See how many rows there are already: 
    NSUInteger rowCount = [table numberOfRowsInSection:0] 
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; 
    for (int i=0; i<3; i++) { 
     NSString *s = [[NSString alloc] initWithFormat:@"%d",i]; 
     [photos addObject:s]; 
     // The new index path is the original number of rows plus i - 1 to leave the last row where it is. 
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i+rowCount - 1 inSection:0]; 
     [indexPaths addObject:indexpath]; 
    } 

    [table beginUpdates]; 
    [table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
    [table endUpdates]; 

    [table reloadData]; 
} 
+0

Inafziger、おかげで動作しません:あなたはその上でインデックス権を取得することができた場合は、ちょうどあなたのindexPathsでこれらのインデックスを使用し、それがすべてうまく、私が思うだろうあなたの助けのために、私はテーブルの最後に行を追加したくない、私はちょうど最後の行の前に追加したい – AlvinZhu

+0

さて、あなたのindexPathを作るときにちょうどrowCount - 1を使用してください。 – lnafziger

+0

次の問題にお手伝いできますか?http://stackoverflow.com/questions/11246562/adding-dynamic-subrows-by-selecting-tableview-row-in-tableview-iphone-errors/11246768#11246768 –

2

わからない私を取得あなたは意味がありますが、私はあなたが抱えている悩みから来るかもしれないと思う[table reloadData][table endUpdates]

+0

sampageが、それは – AlvinZhu

0

後に呼び出す必要はありません、まさに配列の最後の行の前に行を追加するという考え方。追加するたびに、最後の行が変更されます。あなたの答えてくれてありがとう、

NSInteger insertPosition = photos.count - 1; // this index will insert just before the last element 
for (int i=0; i<3; i++) { 
    NSString *s = [[NSString alloc] initWithFormat:@"%d",insertPosition]; // not i 
    [photos insertObject:s atIndex:insertPosition]; 
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:insertPosition inSection:0]; 
    [indexPaths addObject:indexpath]; 
    insertPosition++; // advance it, because the end of the table just advanced 
} 

[table beginUpdates]; 
[table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
[table endUpdates]; 
// no need to reload data as @sampage points out 
+0

ありがとう、それは動作しません – AlvinZhu

関連する問題