0

好的,所以我正在制作一个待办事项列表应用程序。我只是想知道如何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];
}
4

1 回答 1

7

你没有说错误是什么。您应该发布完整的错误并指出哪一行代码实际上导致了错误。

但是您的代码的一个问题是您忘记更新数据源。这需要在更新表视图之前完成。

另一个问题是如何创建索引路径。

像这样的东西:

- (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];
}
于 2013-07-13T00:11:41.223 回答