ios - 将行添加到不带动画的表格 View 的开头

标签 ios objective-c uitableview

我想用begin/endUpdates向tableview添加行,以防止reloadData时tableview跳转

这是我的代码

- (void)updateTableWithNewRowCount:(NSInteger)rowCount
                        andNewData:(NSArray *)newData {
  // Save the tableview content offset
  CGPoint tableViewOffset = [self.messagesTableView contentOffset];

  // Turn of animations for the update block
  // to get the effect of adding rows on top of TableView
  [UIView setAnimationsEnabled:NO];

  [self.messagesTableView beginUpdates];

  NSMutableArray *rowsInsertIndexPath = [[NSMutableArray alloc] init];

  int heightForNewRows = 0;

  for (NSInteger i = 0; i < rowCount; i++) {

    NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
    [rowsInsertIndexPath addObject:tempIndexPath];
//    [self.messages insertObject:[newData objectAtIndex:i] atIndex:i];
      [self addMessage:[newData objectAtIndex:i]];

    heightForNewRows =
        heightForNewRows + [self heightForCellAtIndexPath:tempIndexPath];
  }

  [self.messagesTableView insertRowsAtIndexPaths:rowsInsertIndexPath
                                withRowAnimation:UITableViewRowAnimationNone];

  tableViewOffset.y += heightForNewRows;

  [self.messagesTableView endUpdates];

  [UIView setAnimationsEnabled:YES];

  [self.messagesTableView setContentOffset:tableViewOffset animated:NO];
}

有时(不是每次)我都会收到此错误

 invalid number of rows in section 0.
 The number of rows contained in an existing section after
 the update (2) must be equal to the number of rows contained in that section
 before the update (2), plus or minus the number of rows inserted or deleted
 from that section (2 inserted, 0 deleted) and plus or minus the number of
 rows moved into or out of that section (0 moved in, 0 moved  out).

如何防止此错误?

最佳答案

如果你不需要动画那就直接使用

- (void)updateTableWithNewRowCount:(NSInteger)rowCount
                        andNewData:(NSArray *)newData {
  // Save the tableview content offset
  CGPoint tableViewOffset = [self.messagesTableView contentOffset];

  // Turn of animations for the update block
  // to get the effect of adding rows on top of TableView

  NSMutableArray *rowsInsertIndexPath = [[NSMutableArray alloc] init];

  int heightForNewRows = 0;

  for (NSInteger i = 0; i < rowCount; i++) {

    NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
    [rowsInsertIndexPath addObject:tempIndexPath];
//    [self.messages insertObject:[newData objectAtIndex:i] atIndex:i];
      [self addMessage:[newData objectAtIndex:i]];

    heightForNewRows =
        heightForNewRows + [self heightForCellAtIndexPath:tempIndexPath];
  }

  tableViewOffset.y += heightForNewRows;

  [self.messagesTableView reloadData];

  [self.messagesTableView setContentOffset:tableViewOffset animated:NO];
}

关于ios - 将行添加到不带动画的表格 View 的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776869/

相关文章:

ios - 如何对依赖于 Objective-C 中的 volatile 变量的方法进行单元测试?

ios - 在 UILabel 中显示核心数据结果?

ios - 强制 Objective-C 对象使用某个委托(delegate)

ios - 如何确保在 xcode 4.2 中开发的应用程序将支持 ios 4 以及 ios5

ios - UISplitView 按钮在细节替换 segue 后丢失

ios - 如何在找到页面的最后一项时从下一页加载更多数据

ios - 我如何在 iOS 中按字母顺序对 MediaQuery 的 artistQuery 数组进行排序

ios - 如何在 iOS 中查找字符串并替换它的子字符串?

iphone - 主要在IOS中是如何工作的

ios - UITableView indexPath 部分逻辑