我有一个 UITableView,我想在其中插入和删除行。
如果我有一个包含 10 个值的数组并从包含所有行的 tableview 开始,则该表显示正常。然后,如果我想过滤除 [0, 1, 5] 之外的所有内容。然后我在索引路径删除行的过程: [2, 3, 4, 6, 7, 8, 9] 并且返回的行数是 3。我不必更改我的单元格渲染代码,它显示行:[0, 1, 5]。
但是,如果在过滤完所有内容后,我想返回此表并从持久性中加载包含行 [0, 1, 5] 的表,我遇到了麻烦。表格加载行数:3 并呈现单元格:[0, 1, 2]。
我尝试在 viewDidLoad 中用 0 行初始化表,然后在 viewDidAppear 中调用 insertRows:[0, 1, 5] - 但这会引发异常:
*** -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909
而且我已经验证了在 viewDidLoad 中,打印的行数是 0,而在抛出这个错误之前,打印的行数是 3。
编辑如果我插入 [0, 1, 2] - 它可以工作......但再次......我最终需要 [0, 1, 5] 出现。
- (void) viewDidAppear:(BOOL) 动画
{
[超级 viewDidAppear:动画];
NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]];
[self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 使用不同的布尔值,但要点:
如果(viewDidLoad)
返回0;
if (viewDidAppear || 过滤)
返回 3;
如果(扩展)
返回 10;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
如果(!单元格){
单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
[cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]];
返回单元格;
}