简体   繁体   中英

iOS7 vs iOS8 - insertRowsAtIndexPaths and visible rows issue

I have a UIViewController that contains UITableView. When I tap a row, a method inserts some rows bellow the tapped one. However, if the number of rows exceeds the height of the display, when I scroll on iOS8, I can see all rows. when I scroll on iOS7, it skips some of the rows until I rotate the device. After that the table shows all rows on every rotation. How to make the table to appear the same way in both iOS7 and iOS8? And most important: why does iOS7 cuts the last few rows until the device rotation?

Here is some code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self addRows:indexPath];
}
- (void)addRows:(NSIndexPath *)indexPath
{
    NSInteger numberOfRows = 10;
    NSUInteger count = indexPath.row +1;
    NSMutableArray *indexPathsArray = [NSMutableArray array];

    for(int i = 0; i < numberOfRows; i++) {
        [indexPathsArray addObject:[NSIndexPath indexPathForRow:count inSection:0];
        [self.dataSourceArray insertObject:[NSNumber numberWithInt:i] atIndex:count++];
    }
    [self.tableView insertRowsAtIndexPaths:indexPathsArray withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

ps this is just a demo code, but the principle is the same.

Try this code:

- (void)addRows:(NSIndexPath *)indexPath
{
    NSInteger numberOfRows = 10;
    NSUInteger count = indexPath.row +1;
    NSMutableArray *indexPathsArray = [NSMutableArray array];

    for(int i = 0; i < numberOfRows; i++) {
        [indexPathsArray addObject:[NSIndexPath indexPathForRow:count inSection:0];
        [self.dataSourceArray insertObject:[NSNumber numberWithInt:i] atIndex:count++];
    }

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPathsArray withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM