2

独自のカスタムグループ化UITableViewを作成しました。上のセルが背景として「top.png」、真ん中のセルが「middle.png」、下のセルが「bottom.png」であるテーブルです。

使っています

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

並べ替え中にセルがドラッグされているときに、セルの背景画像を変更します。しかし、私はこれに対する簡単な解決策を見つけることができないようです。しかし、私は常に、多くのif-caseを含む非常に複雑なソリューションになります。それは、すべてのケースを網羅しているわけではないように、非常に厄介で、複雑すぎると感じます。私が監督しているこの問題の簡単な解決策はありますか?

私はここここでこの問題の「解決策」を見つけました。しかし、それらは完全ではなく、すべての場合に機能するとは限りません。

処理する必要があるが、前述の2つのソリューションでは処理できない特殊なケース:

  • 行を移動してから、元の位置に戻します
  • テーブルビューを下にスクロールしながら、行の周りを1行速く移動します
4

1 に答える 1

3

私も同じ問題を抱えていましたが、解決策を見つけたと思います。私のテストでは、1つのセクション(2〜約12個のセルでテスト済み)で正常に機能しますが、異なるセクション間でセルをドラッグする場合は、いくつかの変更が必要になります。その機能は必要ないので、まだこれらの変更を加えていません。

このメソッドは、デリゲートメソッドを実装していることを前提としています

- (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 に答える