moveRowAtIndexPath - 在部分之间移动单元格
Posted
技术标签:
【中文标题】moveRowAtIndexPath - 在部分之间移动单元格【英文标题】:moveRowAtIndexPath - Moving cells between sections 【发布时间】:2015-02-20 20:31:15 【问题描述】:在我的 UITableView 我有这种关系
Department -< Employees
(名称数组)
我已经为每个模型设置了自定义对象。
在我的moveRowAtIndexPath
中,我可以在特定部分移动;但是,如果我尝试在部分之间移动它,它会崩溃。
我希望能够将“汤姆”从“销售”转移到“营销”
#pragma mark - UITableView delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
return [_objects count];
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Department *department = [_objects objectAtIndex:section];
return [department.employees count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];
// Configure the cell...
Department *department = [_objects objectAtIndex:indexPath.section];
Employee *employee = [department.employees objectAtIndex:indexPath.row];
cell.textLabel.text = employee.name;
return cell;
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Department *department = [_objects objectAtIndex:section];
return department.name;
- (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath
return YES;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
if (fromIndexPath == toIndexPath ) return;
Department *department = [_objects objectAtIndex:fromIndexPath.section];
Employee *employee = [department.employees objectAtIndex:fromIndexPath.row];
[self.tableView beginUpdates];
[department.employees removeObjectAtIndex:fromIndexPath.row];
[department.employees insertObject:employee atIndex:toIndexPath.row];
[self.tableView endUpdates];
[tableView reloadData];
我如何获得moveRowAtIndexPath
以允许我将员工从一个部门转移到另一个(或者更确切地说是一个部门到另一个部门)?
非常感谢
//下方崩溃;
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368
2015-02-20 20:51:15.772 Departments[999:90b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. 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 (2), 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).'
*** First throw call stack:
(
0 CoreFoundation 0x01a221e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x017a18e5 objc_exception_throw + 44
2 CoreFoundation 0x01a22048 +[NSException raise:format:arguments:] + 136
3 Foundation 0x013814de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00539f63 -[UITableView _endCellAnimationsWithContext:] + 13402
5 UIKit 0x00549cea -[UITableView endUpdatesWithContext:] + 51
6 UIKit 0x00549d18 -[UITableView endUpdates] + 41
7 Departments 0x00003a48 -[ViewController tableView:moveRowAtIndexPath:toIndexPath:] + 664
8 UIKit 0x005573b4 -[UITableView _endReorderingForCell:wasCancelled:animated:] + 619
9 UIKit 0x006e0c4f -[UITableViewCell _grabberReleased:] + 73
10 UIKit 0x007d1a1b -[UITableViewCellReorderControl endTrackingWithTouch:withEvent:] + 58
11 UIKit 0x005641f1 -[UIControl touchesEnded:withEvent:] + 559
12 UIKit 0x004a2ddd -[UIWindow _sendTouchesForEvent:] + 852
13 UIKit 0x004a39d1 -[UIWindow sendEvent:] + 1117
14 UIKit 0x004755f2 -[UIApplication sendEvent:] + 242
15 UIKit 0x0045f353 _UIApplicationHandleEventQueue + 11455
16 CoreFoundation 0x019ab77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17 CoreFoundation 0x019ab10b __CFRunLoopDoSources0 + 235
18 CoreFoundation 0x019c81ae __CFRunLoopRun + 910
19 CoreFoundation 0x019c79d3 CFRunLoopRunSpecific + 467
20 CoreFoundation 0x019c77eb CFRunLoopRunInMode + 123
21 GraphicsServices 0x031ee5ee GSEventRunModal + 192
22 GraphicsServices 0x031ee42b GSEventRun + 104
23 UIKit 0x00461f9b UIApplicationMain + 1225
24 Departments 0x000046bd main + 141
25 libdyld.dylib 0x02ed5725 start + 0
26 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
【问题讨论】:
什么是崩溃/异常消息?如果您只是要重新加载整个表,那么开始/结束更新也是没有意义的。如果您要在 tableview 上调用离散的删除/插入操作,您只会使用它们 添加了崩溃,movebybefore 未完成 【参考方案1】:您的问题是您已经操纵了数据模型,但您没有告诉 tableview 您所做的更改。
因为您调用beginUpdates
/endUpdates
,tableview 期待一些变化,所以在endUpdates
之后它调用numberOfRowsInSection
- 它返回答案'3' - 但它期待 2+0(因为你没有'不要告诉它有关新行的信息)。
你有几个选择:
1 - 使用moveRowAtIndexPath:toIndexPath:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
if (fromIndexPath != toIndexPath )
Department *department = [_objects objectAtIndex:fromIndexPath.section];
Employee *employee = [department.employees objectAtIndex:fromIndexPath.row];
[tableView beginUpdates];
[department.employees removeObjectAtIndex:fromIndexPath.row];
[department.employees insertObject:employee atIndex:toIndexPath.row];
[tableview moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
[self.tableView endUpdates];
2 - 使用不带 beginUpdate
/endUpdate
的重新加载
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
if (fromIndexPath != toIndexPath )
Department *department = [_objects objectAtIndex:fromIndexPath.section];
Employee *employee = [department.employees objectAtIndex:fromIndexPath.row];
[department.employees removeObjectAtIndex:fromIndexPath.row];
[department.employees insertObject:employee atIndex:toIndexPath.row];
[tableView reloadData];
【讨论】:
我会调查的。谢谢! 我尝试了这两个想法。在第一个中,我遇到了这个奇怪的问题,当我将行移动到另一个部分时,它会突然回到原始部分。在第二个我得到一个崩溃说:NSInternalInconsistencyException',原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(3)必须等于行数更新前包含在该节中 (2) 加上或减去从该节插入或删除的行数(0 插入,0 删除).. 在第二个选项中,不应调用beginUpdate
/endUpdate
,因为您只是重新加载整个表。
另外,我刚刚使用了您的代码,但它看起来不太正确 - 您的目标是将员工从一个部门转移到另一个部门,但只引用了一个部门 - 你不应该从toIndex.section
得到另一个部门?
是的,我想将employee
从一个department
移动到另一个;我认为您可能是对的,仅引用了 1 个部分;这也许可以解释为什么我让事情恢复到原来的状态;我会尝试调查【参考方案2】:
感谢@Paulw11,解决方案是我没有考虑departmentFrom和departmentTo,因为我想要的是Move Tom from Sales to Marketing
;
因此我的解决方法是:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
if (fromIndexPath != toIndexPath )
Department *departmentFrom = [_objects objectAtIndex:fromIndexPath.section];
Department *departmentTo = [_objects objectAtIndex:toIndexPath.section];
Employee *employee = [departmentFrom.employees objectAtIndex:fromIndexPath.row];
[departmentFrom.employees removeObjectAtIndex:fromIndexPath.row];
[departmentTo.employees insertObject:employee atIndex:toIndexPath.row];
[tableView reloadData];
接受 Paulw11 的回答。
【讨论】:
以上是关于moveRowAtIndexPath - 在部分之间移动单元格的主要内容,如果未能解决你的问题,请参考以下文章
使用tableView:moveRowAtIndexPath:toIndexPath:方法后,将新订单保存到核心数据
UITableView: 使用 moveRowAtIndexPath:toIndexPath: 和 reloadRowsAtIndexPaths:withRowAnimation: 一起出现损坏