gpt4 book ai didi

ios 使用 moveRowAtIndexPath :toIndexPath: correctly

转载 作者:行者123 更新时间:2023-11-28 18:11:30 27 4
gpt4 key购买 nike

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

最佳答案

你不说错误是什么。您应该发布完整的错误并指出实际导致错误的代码行。

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

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

像这样:

- (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];
}

关于ios 使用 moveRowAtIndexPath :toIndexPath: correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17625680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com