【问题标题】:ios 5: Invalid update: invalid number of rows in sectionios 5:无效更新:部分中的行数无效
【发布时间】:2013-07-23 10:34:40
【问题描述】:

我只有 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 中,删除某些行和某些部分后,我遇到了这个问题。

你能帮帮我吗?

【问题讨论】:

  • 我认为 [dictionary allKeys] 中的问题我在 (ios 5) 中没有,allKeys 返回键的随机顺序只是检查在这些语句中放置断点。(我不确定这只是试试)
  • 但在 iOS 5 中,删除某些行和某些部分后,我遇到了这个问题。
  • 只需添加 [self.tableView reloadData];和 self.editing = YES;这是必需的,因为表视图最初没有关于其数据源和委托的信息;如果你创建一个表视图,它总是需要在初始化过程中发送一个 reloadData 消息。
  • 使用[字典计数];每个地方(不确定)
  • 你的 dictionary 是 NSMutableDictionary 对象吗?

标签: iphone ios uitableview ios6 tablerow


【解决方案1】:

需要补充

[self.tableView reloadData];

self.editing = YES;

这是必需的,因为表格视图最初没有关于其数据源和委托的信息;如果你创建一个表视图,它总是需要在初始化过程中发送一个 reloadData 消息。

希望对你有帮助

【讨论】:

  • 删除10节后还是有这个问题
  • 还建议您检查您的 tableView 属性是否正确连接到您的接口 XIB 文件中的 UITableView。做 NSLog(@"%@", self.tableView);在您的 viewWillAppear: 中,为了获得更新的列表,除了 self.tableView reloaddata 之外,还需要再次调用 viewdidLoad 方法。希望对你有帮助
【解决方案2】:

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

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

【讨论】:

  • - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [字典计数];我尝试使用它,但没有任何改变
  • 用 OrderedDictionary 替换 NSMutableDictionary 是个好方法。你救了我的命。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 2015-07-01
  • 2022-11-22
  • 2019-03-11
  • 2014-10-28
相关资源
最近更新 更多