我有同样的问题,我想我已经找到了解决方案。从我的测试来看,它在一个部分中运行良好(测试了两个到大约十几个单元格),但如果您在不同部分之间拖动单元格,则需要进行一些修改。我还没有进行这些修改,因为我不需要那个功能。
此方法假定您已实现委托方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
设置单元格的初始背景并确保其正常工作。
此方法将在单元格移动时更新其新位置
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
// Get the index paths of the cells above and below our current target cell
NSIndexPath *higherPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row - 1 inSection:proposedDestinationIndexPath.section];
NSIndexPath *lowerPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row + 1 inSection:proposedDestinationIndexPath.section];
// Update our source cell to the appropriate image for its current position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:sourceIndexPath] forRowAtIndexPath:proposedDestinationIndexPath];
if (sourceIndexPath.row > proposedDestinationIndexPath.row) {
// Source cell is below the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:lowerPath];
// Ensure that any cell above our new target is refreshed if the cell is dragged back down the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
} else if (sourceIndexPath.row < proposedDestinationIndexPath.row) {
// Source cell is above the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:higherPath];
// Ensure that any cell below our new target is refreshed if the cell is dragged back up the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
} else {
// Source cell is the same as the target - cell is back in its original position
// Update any cells below and above the position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
}
return proposedDestinationIndexPath;
}
我在患流感时想出了这个,所以我可能在解释中犯了一些愚蠢的错误,但我非常有信心这应该可靠地工作,除了单节限制。