我有一个多节的tableview。在编辑模式中,我允许将行从一个部分移动到另一个部分。一旦最后一行从一个部分删除,我删除该部分。所以我使用moveRowAtIndexPath中的deleteSection。将最后一行移出部分和删除部分时的奇怪动画
当最终项目从该部分移开时,部分标题按计划消失。但是有一个非常奇怪的动画错误,其中被移动的行似乎与其上面放置的行“合并”,并且在“to”部分的底部显示了空行(可能是因为该部分的numberOfRows是正确,但2行在同一位置)。更奇怪的是,当我点击该行的重新排序控件(不移动项目,只是触摸并释放)时,两个项目“unmerge”。
我发布了一个video演示这个。
我试过包装我的数据更改并查看开始/结束更新中的更改,但无济于事。
我上传了一个测试项目here,我也会发布下面的代码。几点:
- 我试图在演示项目中复制我的数据源的格式,以防发生问题。关键是我的源码是两个其他数组的复合数组(尽管我不明白为什么这会是一个问题)。
- 要查看有问题的行为,请将底部的两行移动到顶部。不要将它们放在顶部的最后一行,因为这似乎工作正常。
- 从上面的部分到下面的部分以另一种方式移动行,在这个演示项目中是错误的。
代码(这一切都是在演示项目):
我的loadView设置我的数组:
- (void)loadView{
array1 = [NSMutableArray array];
[array1 addObject:@"test 0"];
[array1 addObject:@"test 1"];
[array1 addObject:@"test 2"];
array2 = [NSMutableArray array];
[array2 addObject:@"test a"];
[array2 addObject:@"test b"];
[super loadView];
}
我也有一个返回的这些组合的方法阵列 - 这是用作数据源:
- (NSMutableArray *)sourceArray{
NSMutableArray *result = [NSMutableArray array];
if (array1.count > 0) {
[result addObject:array1];
}
if (array2.count >0) {
[result addObject:array2];
}
return result;
}
它允许非常简单的ro WS /部分:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.sourceArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[self.sourceArray objectAtIndex:section] count];
}
标准单元/头文件格式:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [[self.sourceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:@"Section %i", section];
}
这是你的行或里面有什么,我做魔术
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSMutableArray *fromArray = [self.sourceArray objectAtIndex:fromIndexPath.section];
NSMutableArray *toArray = [self.sourceArray objectAtIndex:toIndexPath.section];
NSString *movedObject = [[self.sourceArray objectAtIndex:fromIndexPath.section] objectAtIndex:fromIndexPath.row];
[fromArray removeObject:movedObject];
[toArray insertObject:movedObject atIndex:toIndexPath.row];
if ([self.tableView numberOfRowsInSection: fromIndexPath.section] == 0) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:fromIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
你为什么要-loadView设置您的阵列?如果以编程方式创建视图,则应该只覆盖-loadView。没有?并且调用[super loadView]尝试加载与所讨论的视图控制器相同名称的笔尖。为什么不把你的数组设置移动到-viewDidLoad? – 2012-04-03 01:58:43
感谢 - 任何链接/更多信息为什么这是/这个规则来自哪里?当然没有帮助我的问题,但谢谢你的提示。 – 2012-04-03 02:18:35
,因为如果您在nib中设置视图,loadView会被操作系统调用。该方法的文档明确指出不直接调用此方法,并且不要覆盖如果从nib加载视图。 – jmstone617 2012-04-04 16:06:06