繁体   English   中英

insertRowsAtIndexPaths中的NSInternalInconsistencyExceptionException

[英]NSInternalInconsistencyException in insertRowsAtIndexPaths

我正在尝试为我的表视图实现insertRowsAtIndexPaths

在此实现之前,我曾经在用户点击表末尾后调用[self.tableView reloadData] 这种方法有点影响了我的应用程序的质量,因为它需要进行大量的行和大量的滚动。(每次重新冻结之前都会重新加载数据)

所以我改用下面的代码:

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

旧的工作代码是:

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

p.appRecordList是一个NSArray ,新行项目来自hayoola。

对于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;
}

我的代码中也没有cellforRowAtIndexPath 我应该提一下,我的程序每次都会从服务r获取10行。

这是控制台错误代码:

由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“试图将第10行插入第0节,但更新后第0节只有10行” *

编辑:(因答案而异)

在将更新方法更改为此之后:

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

并为此insertRowsAtIndexPaths

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

出现此错误:

由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第0节中的行数无效。更新(20)之后现有节中包含的行数必须等于该节中包含的行数更新前的部分(10),加上或减去从该部分插入或删除的行数(已插入1,已删除0)以及正负移动到该部分的行数(移入0,已移动0)出)。“*

您需要确保在调用endUpdates之前已将其他记录添加到self.entries 例外是告诉您您尝试添加第11行,但numberOfRowsInSection返回10

您需要确保插入的行数与添加到数组中的元素的行数相同

您的更新方法应为

[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