【发布时间】:2010-01-13 08:45:26
【问题描述】:
我有一个表格,它显示来自单个数组的数据,并根据一组过滤器将项目组织到单独的部分中。每个过滤器都有一个相应的部分。我的 UI 允许用户点击嵌入在每个表格单元格中的复选框。此复选框设置一个“选中”标志,该标志会影响项目被过滤到的部分。这是一个例子:
- 第 1 节
- 项目 A
- B 项
- 第 2 节
- “完成”部分
点击项目 A 旁边的复选框应该会使表格重新排列,如下所示:
- 第 1 节
- B 项
- 第 2 节
- “完成”部分
- 项目 A
下面是我响应复选框点击的代码。它确定复选框属于哪一行,然后尝试创建一个动画块,从其旧部分中删除该行并在“完成”部分的末尾添加一行。请记住,数组实际上并没有改变——数据被过滤到部分的方式会根据项目的选中属性的值而改变。
- (IBAction)checkBoxTapped:(id)sender
{
// Get the table cell
CheckBoxControl* checkBox = (CheckBoxControl*)sender;
UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
UITableView* table = (UITableView*)[cell superview];
NSIndexPath* indexPath = [table indexPathForCell:cell];
Item* item = [self itemAtRow:[indexPath row] inSection:[indexPath section]];
item.checked = checkBox.checked;
Filter* filter = [filters objectAtIndex:DONE_INDEX];
// We want the new row at the bottom of the "Done" section.
NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:[self countItemsWithFilter:filter] inSection:DONE_INDEX];
[table beginUpdates];
[table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[table endUpdates];
}
这段代码总是抛出异常:
2010-01-13 00:19:44.802 MyApp[19923:207] *** Terminating app due to uncaught
exception 'NSRangeException', reason:
'*** -[NSMutableIndexSet addIndexesInRange:]:Range {2147483647, 1}
exceeds maximum index value of NSNotFound - 1'
谁能帮我找出导致 NSRangeException 的原因?这是我的堆栈:
___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___
obj_exception_throw
+[NSException raise:format:arguments:]
+[NSException raise:format:]
-[NSMutableIndexSet addIndexesInRange:]
-[NSMutableIndexSet addIndex:]
-[_UITableViewUpdateSupport(Private) _computeRowUpdates]
-[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:oldRowRange:newRowRange:context:]
-[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:]
-[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:]
-[UITableView endUpdates]
-[RootViewController checkBoxTapped:]
如果这段代码完全错误,那么我应该如何动画化从一个部分中删除一行并将其添加到另一个部分?
提前感谢您能给我的任何帮助。
-迈克-
【问题讨论】:
标签: iphone uitableview