1

我有一个有几个section的UITableView,section的数量是动态的,也就是可以随时增加。在表视图上重新加载带有动画的部分,其中部分的数量不是什么大问题。我只是这样称呼:

[notesTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

但是,当部分数量发生变化时,让这个工作变得非常痛苦。我总是得到那个典型的断言错误"The number of sections in your tableview before the reload must be the same as after the reload..etc"。我知道我应该用[tableView insertSections...]它来摆脱这个,但在我的情况下这样做可能很困难。

我的部分数量取决于数组的大小。无论如何都可以自动重新加载带有动画的部分,就像调用一样简单[tableView reloadData],而不必担心手动插入行和部分?

(我知道一个廉价的解决方法是使用 UIView 动画或 CAAnimation,但这与 reloadSections withAnimation 方法的效果并不完全相同。)

4

3 回答 3

1

这是您在玩弄FetchedViewControllers. 您收到该错误的原因是因为您正在删除 中的对象dataSource,并且 tableView 尝试加载 tableView,并查看 delegateMethods - 它表明您的部分少于指定的部分。

你有两个选择:

  1. 实现FetchedResultsController委托方法并实现objectWillChange(或)objectDidChange方法,在里面说类似

    [[self tableView] reloadData]; 
    
    // or
    
    [[self tableView] endUpdates];
    

    那应该可以解决问题。

  2. 实现通知(当您删除不同 managedObjectContexts 下的数据时),并在NSManagedObjectContextDidSave(or)时发布通知NSManagedObjectContextDidChange。然后你必须添加一个观察者来合并通知的变化。但我怀疑你的问题是否严重。我想说选项(1)应该足够了。

于 2012-04-07T16:00:15.563 回答
0

//创建NSMutableSet [self.tableView beginUpdates]; if (isAdd) { [self.tableView insertSections:insertIndexSet withRowAnimation:UITableViewRowAnimationBottom]; } else{ [self.tableView deleteSections:insertIndexSet withRowAnimation:UITableViewRowAnimationTop]; } [self.tableView endUpdates];

于 2017-04-08T09:04:42.563 回答
-2

我不知道这是否适用于动画,但它应该......

在您的 UItableView 实现中,将您的numberOfSections方法更改为如下所示:

- (NSInteger)numberOfSections
{
    return numberOfSections;
}

numberOfSections是一个 NSInteger,它会在需要时更改。

更改此号码后尝试拨打:

[tableView reloadData];

你的 UITableView 应该相应地改变。

编辑:我将它与动画一起使用,但我再次不确定它是否适用于您的情况。它适用于我的编辑风格、插入和删除行以及对我的内容进行一般更新。

当您需要更改 UITableView 内容时,请输入以下内容:

[tableView beginUpdates];
// Here change your contents
[tableView endUpdates];
于 2012-04-07T15:35:51.000 回答