1

I want to insert many rows in front of the last row in a tableview at the same time, but it add up a row in front of the last row and add up two more rows at the end. How to figure it out? Please help me, thank you in advance. !

- (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 Answers 3

4
- (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];
}
3
  • Inafziger, thanks for your help, but I do not want to add rows in the end of the table, I just want to add before the last row
    – AlvinZhu
    Mar 21, 2012 at 15:23
  • Okay, just use rowCount - 1 when making your indexPath them.
    – lnafziger
    Mar 21, 2012 at 15:50
  • Can you help me on following issue stackoverflow.com/questions/11246562/… Jul 11, 2012 at 14:07
2

Not sure I get exactly what you mean but you shouldn't need to call [table reloadData] after [table endUpdates]

0
0

I think the trouble you're having might be coming from the idea of adding rows before the last row of an array. Each time you add, the last row changes. If you can get the indexing right on that, then just use those indexes in your indexPaths, it will all work out, I think:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.