好的,所以我正在做一个待办事项列表应用程序.我只是想知道如何moveRowAtIndexPath:toIndexPath:正确使用,因为如果toDoItemCompleted方法被触发它会一直崩溃.触发方法后,我试图将行向下移动到列表的底部.
-(void)toDoItemCompleted:(ToDoItem *)todoItem {
NSUInteger origIndex = [_toDoItems indexOfObject:todoItem];
NSIndexPath *origIndexPath = [[NSIndexPath alloc]initWithIndex:origIndex];
NSUInteger endIndex = _toDoItems.count-1;
NSIndexPath *endIndexPath = [[NSIndexPath alloc]initWithIndex:endIndex];
[self.tableView beginUpdates];
[self.tableView moveRowAtIndexPath:origIndexPath toIndexPath:endIndexPath];
[self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
你没有说出错误是什么.您应该发布完整错误并指出哪行代码实际导致错误.
但是代码的一个问题是您忘记更新数据源.这需要在更新表视图之前完成.
另一个问题是如何创建索引路径.
像这样的东西:
- (void)toDoItemCompleted:(ToDoItem *)todoItem {
NSUInteger origIndex = [_toDoItems indexOfObject:todoItem];
NSIndexPath *origIndexPath = [NSIndexPath indexPathForRow:origIndex inSection:0];
NSUInteger endIndex = _toDoItems.count - 1;
NSIndexPath *endIndexPath = [NSIndexPath indexPathForRow:endIndex inSection:0];
// Update date source
[_toDoItems removeObject:todoItem]; // remove from current location
[_toDoItems addObject:todoItem]; // add it to the end of the list
[self.tableView beginUpdates];
[self.tableView moveRowAtIndexPath:origIndexPath toIndexPath:endIndexPath];
[self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3150 次 |
| 最近记录: |