ios - 如何在tableview中同时插入多行?

标签 ios iphone xcode

我想在一个 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];
}

关于ios - 如何在tableview中同时插入多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798755/

相关文章:

ios - 在 UIGestureRecognizer 中双击

iphone - 如何加载本地化 Nib ?

c# - 您如何同时为*所有*手机/移动设备写作?

ios - 如何在 AVFoundation 中混合/屏蔽视频?

objective-c - 在自定义单元格中维护 UITextViews 的值

ios - 如何更改 xcode 中的 .mobileprovision

ios - ReactiveSwift - 将 `Action` 绑定(bind)到 `UIControl`

iphone - 弹出 Controller 时 UINavigationController 内存不会减少

iphone - 从 xcode 项目中删除不必要的图像

ios - 从 loginviewcontroller 获取用户邮箱并显示在 revealview 的标签中