简体   繁体   English

iOS7 vs iOS8-insertRowsAtIndexPaths和可见行问题

[英]iOS7 vs iOS8 - insertRowsAtIndexPaths and visible rows issue

I have a UIViewController that contains UITableView. 我有一个包含UITableView的UIViewController。 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. 但是,如果行数超过了显示屏的高度,则在iOS8上滚动时,我可以看到所有行。 when I scroll on iOS7, it skips some of the rows until I rotate the device. 当我在iOS7上滚动时,它会跳过某些行,直到我旋转设备为止。 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? 如何使表格在iOS7和iOS8中以相同的方式出现? And most important: why does iOS7 cuts the last few rows until the device rotation? 最重要的是:为什么iOS7会削减最后几行直到设备旋转?

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. ps这只是一个演示代码,但原理是相同的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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