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

时间:2017-06-30 13:31:31

标签: ios objective-c uitableview

我在向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的部分中。所有其他部分都很好

2 个答案:

答案 0 :(得分:0)

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

//MARK: UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 7) {

    return YOUR_ARRAY.count;
 }
}

答案 1 :(得分:0)

首先需要在集合中添加对象,告诉tableview关于出现的项目的数量,为新单元格创建索引路径并使用索引路径插入行。也许你创建了错误的索引路径。