2

我创建了自己的自定义分组 UITableView。这是一个表格,顶部单元格使用“top.png”作为背景,中间单元格使用“middle.png”,底部单元格使用“bottom.png”。

我在用着

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

在重新排序期间拖动单元格时更改单元格的背景图像。但是,我似乎无法找到一个简单的解决方案。不管我怎么尝试,我总是会得到一个非常复杂的解决方案,其中包含很多 if 案例,而且感觉真的很混乱,过于复杂,就像我没有涵盖所有案例一样。我正在监督这个问题是否有一个简单的解决方案?

我在这里这里找到了这个问题的“解决方案” 。但它们并不完整,并不适用于所有情况。

应处理的特殊情况,但上述两种解决方案无法处理:

  • 移动行,然后回到原来的位置
  • 在行周围快速移动一行,向下滚动表格视图
4

1 回答 1

3

我有同样的问题,我想我已经找到了解决方案。从我的测试来看,它在一个部分中运行良好(测试了两个到大约十几个单元格),但如果您在不同部分之间拖动单元格,则需要进行一些修改。我还没有进行这些修改,因为我不需要那个功能。

此方法假定您已实现委托方法

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

我在患流感时想出了这个,所以我可能在解释中犯了一些愚蠢的错误,但我非常有信心这应该可靠地工作,除了单节限制。

于 2013-04-05T04:54:07.070 回答