0Pricing
SwiftUI Academy · レッスン

onMove による並べ替え

行をドラッグして並べ替えます。

「onMove による並べ替え」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Let Users Reorder Rows

Sometimes order matters, like a to-do priority list. SwiftUI lets people drag rows into a new order with almost no extra code. ✨

Meet onMove

The onMove modifier sits on your ForEach and enables drag handles so rows can be picked up and dropped elsewhere.

The Move Signature

onMove gives you two things: an IndexSet of the rows being moved and an Int destination index where they should land.

func move(from source: IndexSet,
          to destination: Int) {
}

One Array Method Does It

Swift arrays have a built-in move(fromOffsets:toOffset:) that performs the reorder for you in a single, safe call.

items.move(fromOffsets: source,
           toOffset: destination)

Wiring onMove Up

Attach onMove(perform:) to the ForEach and point it at your move function. The list now supports drag-to-reorder.

ForEach(items) { item in
    Text(item.name)
}
.onMove(perform: move)

Edit Mode Reveals Handles

Drag handles often appear only in edit mode. An EditButton in the toolbar flips the list into editing so users can reorder.

.toolbar {
    EditButton()
}

Reorder Without Edit Mode

Inside a plain List, rows are frequently draggable straight away. Edit mode mainly matters when you also expose delete and move controls.

State Keeps Order Persistent

Because move() mutates your @State array, the new order survives view refreshes and reflects everywhere that array is read.

Combine Move and Delete

You can attach both onMove and onDelete to the same ForEach, giving users full control to reorder and remove rows.

ForEach(items) { Text($0.name) }
    .onDelete(perform: delete)
    .onMove(perform: move)

Inline Move Closure

Like delete, you can write the reorder inline. Just call move(fromOffsets:toOffset:) directly inside the onMove closure.

.onMove { source, dest in
    items.move(fromOffsets: source,
               toOffset: dest)
}

Smooth Drag Animations

SwiftUI animates the lift, slide, and settle of dragged rows automatically. You get polished motion with zero animation code.

Quick Check

What does onMove provide to your handler?

Recap: Reordering

You added drag-to-reorder by attaching onMove to a ForEach and calling move(fromOffsets:toOffset:) on your state array. 🎉

よくある質問

「onMove による並べ替え」レッスンは無料ですか?

はい。「onMove による並べ替え」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「onMove による並べ替え」で何を学びますか?

行をドラッグして並べ替えます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「onMove による並べ替え」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. スワイプで行を削除する
  2. onMove による並べ替え
  3. 新しい項目の追加
  4. プルして更新
← SwiftUI Academyに戻る