简体   繁体   English

insertRowsAtIndexPaths中的NSInternalInconsistencyExceptionException

[英]NSInternalInconsistencyException in insertRowsAtIndexPaths

I'm trying to implement insertRowsAtIndexPaths for my table view. 我正在尝试为我的表视图实现insertRowsAtIndexPaths

Before this implementation, I used to call [self.tableView reloadData] after user hit end of table. 在此实现之前,我曾经在用户点击表末尾后调用[self.tableView reloadData] This method kinda impacted my app quality for lots of row and lots of scrolling.(freezing each time before reloading data) 这种方法有点影响了我的应用程序的质量,因为它需要进行大量的行和大量的滚动。(每次重新冻结之前都会重新加载数据)

So I used this code instead: 所以我改用下面的代码:

p.lastid = appRecord.ids;
[p main];
dispatch_async(dispatch_get_main_queue(), ^(void)
        {                                    
         [self.tableView beginUpdates];
         NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
         [self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
         [self.tableView endUpdates];

          });
      });
    }
    return cell;
}

The old working code was: 旧的工作代码是:

  NSArray *temp =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
  self.entries = temp;
  [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

p.appRecordList is an NSArray and the new row item is from hayoola fetch. p.appRecordList是一个NSArray ,新行项目来自hayoola。

For numberOfRowInSection I use this: 对于numberOfRowInSection我使用以下代码:

#define kCustomRowCount     10
@property (nonatomic, assign) int intindex;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger count = [self.entries count];
    if (count == 0)
    {
        return kCustomRowCount;
    }
    return count;
}

Also there is no cellforRowAtIndexPath in my code. 我的代码中也没有cellforRowAtIndexPath I should mention that my program will fetch 10 row each time from serve r. 我应该提一下,我的程序每次都会从服务r获取10行。

This is the Console error code: 这是控制台错误代码:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 10 into section 0, but there are only 10 rows in section 0 after the update'* 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“试图将第10行插入第0节,但更新后第0节只有10行” *

edit :(change due to answer) 编辑:(因答案而异)

after changing the update method to this : 在将更新方法更改为此之后:

[self.tableView beginUpdates];
NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp_1;
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

and insertRowsAtIndexPaths to this : 并为此insertRowsAtIndexPaths

    NSUInteger count = [self.entries count];
    return count;

this error appears : 出现此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (20) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'* 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第0节中的行数无效。更新(20)之后现有节中包含的行数必须等于该节中包含的行数更新前的部分(10),加上或减去从该部分插入或删除的行数(已插入1,已删除0)以及正负移动到该部分的行数(移入0,已移动0)出)。“*

You need to ensure that the additional record(s) have been added to self.entries before the call to endUpdates . 您需要确保在调用endUpdates之前已将其他记录添加到self.entries The exception is telling you that you tried to add an 11th row, but numberOfRowsInSection returned 10 例外是告诉您您尝试添加第11行,但numberOfRowsInSection返回10

You need ensure that you insert the same number of rows as elements added to your array 您需要确保插入的行数与添加到数组中的元素的行数相同

Your update method should be 您的更新方法应为

[self.tableView beginUpdates];

NSMutableArray *newIndices=[NSMutableArray new];

for (id record in p.appRecordList) {
   [self.entries addObject:record];
   [newIndicies addObject:[NSIndexPath indexPathForRow:self.entries.count]];
}


[self.tableView insertRowsAtIndexPaths:newIndices withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

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

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