- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 UITableView 中,我有这种关系
Department -< Employees
(名称数组)
我已经为每个模型设置了自定义对象。
在我的 moveRowAtIndexPath
,我可以在特定的部分移动;但是,如果我尝试在各个部分之间移动它,它就会崩溃。
我希望能够将“Tom”从“销售”转移到“营销”
#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 你所做的更改。
因为您调用了 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];
}
}
关于ios - moveRowAtIndexPath - 在部分之间移动单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637660/
iOS 5 最终为 UITableView 添加了 moveRowAtIndexPath:toIndexPath: 方法。问题是,调用它只会移动行,不会更新/重绘它。唯一的方法似乎是找到单元格并直接调
我有一个tableview,其中包含两个部分:要购买的成分和用户已经拥有的成分。因此,基本上,当使用购物时,他/她会将一种成分从一个列表传递到另一个列表。 这里是tableView:cellForRo
在我的 UITableView 中,我有这种关系 Department -< Employees (名称数组) 我已经为每个模型设置了自定义对象。 在我的 moveRowAtIndexPath ,我可
我需要 UITableview 方面的帮助,特别是 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
我有一个字符串数组,已经在我的 uitableview 中显示了。 当为我的uitableview启用编辑时,我试图将字符串从第四行移动到第三行,当我调试它时,我在代码索引部分的插入对象中得到EXC_
我有一个 UITableView我将一行从可见滚动区域外移动到 UITableView 的顶部.动画按预期工作,但最终显示了以前索引 0 处的重复单元格。 代码看起来像这样,除了硬编码的索引: [se
我有一个 UITableViewController 显示动态表格数据,最后一行(使用单独的单元格原型(prototype))包含一个“添加”按钮。 我希望用户能够对表格行重新排序,但阻止他们将行移动
有什么方法可以在-[UITableView moveRowAtIndexPath:toIndexPath]期间执行的动画完成后执行方法? 即,我希望 TableView 以动画方式将单元格移动到其新的
我有一个简单的 UITableView Controller ,用于显示 CoreData。我正在尝试实现 - (void)tableView:(UITableView *)tableView mov
我有一个包含 7 个 TableView 的 View ,代表一周中的 7 天。每个 tableView 都使用 0 到 6 的标签进行初始化。我在单元格中创建了一个 longPressure 识别器
我需要在 UITableView 按钮的 didSelectRowAtIndexPath 和 moveRowAtIndexPath 方法之间进行检测,但我发现当我设置 [myTableView set
在我的应用程序中,我使用了一个填充有核心数据对象的 UITableView。我想让改变单元格的顺序成为可能。所以我实现了 -(BOOL)tableView:(UITableView *)tableVi
我实现了 moveRowAtIndexPath:toIndexPath: 方法的主体,如下所示: - (void)tableView:(UITableView *)tableView moveRowA
好的,我正在制作一个待办事项列表应用程序。我只是想知道如何正确使用 moveRowAtIndexPath:toIndexPath:,因为如果 toDoItemCompleted 方法被触发,它会一直崩
目前我在我的自定义表格 View 中显示了一个简单的对象数组,我也使用了SwipyCell测试滑动手势。 然后我尝试使用 moveRowAtIndexPath 对对象重新排序,但是由于某些原因它不适用
在我的 UITableView 中,我有一个特定的单元格 (A),其外观取决于同一部分中所有其他单元格的顺序。因此,每当用户将单元格拖动到表格 View 中的新位置时,我都需要更新单元格 (A) 的内
我在 Swift iOS 应用程序中有一个 tableView,它允许用户对行重新排序。点击“编辑”按钮,可以重新排序或删除行,然后再次点击“编辑”按钮(现在显示为“完成”)以完成该过程。 如果我只是
当我移动其他单元格时,我需要保持我的第一个单元格始终位于 tableview 的顶部。我花了很多时间和多种方式按钮,我还没有弄清楚如何解决这个问题。 这是我的代码: -(UITableViewCell
我想使用 iOS 5 漂亮的行移动调用来为 TableView 设置动画以匹配某些模型状态更改,而不是旧式的删除和插入。 更改可能包括重新排序和就地更新,我想为两者设置动画,因此某些行将需要 relo
当使用 moveRowAtIndexPath:toIndexPath: 移动 UITableViewCell 时,我在使用 estimatedRowHeight 和 UITableViewAutoma
我是一名优秀的程序员,十分优秀!