我想同时在tableview的最后一行的前面插入很多行,但是它在最后一行的前面增加了一行,并在末尾又增加了两行。如何解决?请帮助我,谢谢你。 !

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

最佳答案

- (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];
}

10-07 23:49