iOS - 将行添加到 UITable (Objective-C) 时出错



我在向 UITable 添加行时收到此错误。

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

元素数量:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
return mArray[section];
}
return 1; // only top row showing
}
// Return the number of rows in the section.
return 1;
}

计算每个部分中的数字:

int k = 0;
for (int i = 0; i < 28; i++) {
if ([[currentEntry objectForKey:@"item"] isEqualToString:items[i]]) {
mArray[i] += 1;
k = i;
}
}

添加到表

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];

if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
NSLog(@"SSection:%d", rows);
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}

此处发生错误

else
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}
}
}
}

此错误仅发生在一个部分(即 7(中。所有其他部分都很好

首先,您需要在集合中添加对象,该对象告诉表视图有关要显示的项目计数,为新单元格创建索引路径并使用索引路径插入行。也许您创建了错误的索引路径。

我认为您的arraynumberOfRows应该在第 7 部分中匹配。

//MARK: UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 7) {
return YOUR_ARRAY.count;
}
}

最新更新