iphone - iOS 5:更新无效:部分中的行数无效

标签 iphone ios uitableview ios6 tablerow

我在iOS 5上只有这个问题,在iOS 6上没有问题

这是我的日志

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 3.  The number of rows contained in an existing section after the update (3) 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)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[dictionary allKeys] count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *keyArray = [dictionary allKeys];
    return [[dictionary objectForKey:[keyArray objectAtIndex:section]] count];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
=    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        //First get all the keys of dictionary into one array
        NSArray *sectionsArray = [dictionary allKeys];
        //Get all the data of tapped section into one array by using indexpath.section
        NSMutableArray *objectsAtSection = [dictionary objectForKey:[sectionsArray objectAtIndex:indexPath.section]];
        //remove the particular object by using indexPath.row from the array
        [objectsAtSection removeObjectAtIndex:indexPath.row];
        // Update dictionary

        [table beginUpdates];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        if ([objectsAtSection count] > 0)
        {
            [dictionary setObject:objectsAtSection forKey:[sectionsArray objectAtIndex:indexPath.section]];
            // Section is not yet empty, so delete only the current row.
            // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]
            [table deleteRowsAtIndexPaths:@[indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [dictionary removeObjectForKey:[sectionsArray objectAtIndex:indexPath.section]];
            // Section is now completely empty, so delete the entire section.
            [table deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                     withRowAnimation:UITableViewRowAnimationFade];
        }
        [table endUpdates];
    }
}

在iOS 5中,删除某些行和某些部分后,我遇到了这个问题。

请你帮助我好吗?

最佳答案

好的,看来我找到了答案。参见this SO question并回答,解释为什么使用allKeys方法不好。

只要您不添加或删除字典中的任何元素,它们将保持相同的顺序,但是一旦您添加或删除元素,新的顺序将完全不同。

关于iphone - iOS 5:更新无效:部分中的行数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807716/

相关文章:

ios - 快速选择音频设备输出(如 Waze)

ios - 在 UILabel 中,是否可以强制一条线在某个地方不中断

ios - UITableViewCell 的背景颜色

javascript - 如何在自动放大表单后触发 iPhone 缩小?

iphone - Ruby on Rails View 的移动版本

iOS UILabel 使用负背景色作为文本颜色

ios - 在 UITableView 中拖放整个部分而不是单行

iOS Storyboard - 创建自定义的 UITableViewCell

iphone - 使用 App store 对可执行文件进行了无效授权签名

objective-c - 我可以在同一个 session 中使用 AVCaptureVideoDataOutput 和 AVCaptureMovieFileOutput 吗?