0Pricing
SwiftUI Academy · レッスン

新しい項目の追加

行を追加し、リストの状態を同期させます。

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

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

Growing the List

A useful list does not stay frozen. Letting users add items is the other half of editing, and it keeps your @State array in charge. ✨

Append to Your Array

Adding a row is just adding data. Call append on your @State array and SwiftUI inserts a fresh row automatically.

items.append(Item(name: "Bread"))

A Toolbar Add Button

Most apps add items from a plus button. Put a Button in the toolbar that calls your add logic when tapped.

.toolbar {
    Button("Add") { addItem() }
}

Use a System Plus Icon

Swap the text for an SF Symbol so it reads as Add. The plus symbol is the universal cue users already understand.

Button {
    addItem()
} label: {
    Image(systemName: "plus")
}

Writing addItem

Your addItem function creates a new model value and appends it. Keep it small and let the array drive the UI.

func addItem() {
    items.append(Item(name: "New"))
}

Add From a TextField

Often the new item comes from typing. Bind a TextField to a draft string, then append that text when the user confirms.

@State private var draft = ""
TextField("Item", text: $draft)

Append Then Clear

After appending, reset the draft so the field is empty for the next entry. Clearing draft keeps the form feeling fresh.

items.append(Item(name: draft))
draft = ""

Guard Against Blanks

Do not add empty rows. A quick guard on the trimmed draft stops accidental blank items from cluttering your list.

guard !draft.trimmingCharacters(
    in: .whitespaces).isEmpty
else { return }

New Rows Animate In

Because the array changed, SwiftUI slides the new row into place. Wrap the append in withAnimation for an even snappier feel.

withAnimation {
    items.append(Item(name: draft))
}

Insert at the Top

Want newest first? Use insert(at:) with index 0 instead of append, and the row appears at the top of the list.

items.insert(Item(name: draft),
             at: 0)

One Source of Truth

Add, delete, and move all mutate the same @State array. That single source of truth keeps your whole list consistent.

Quick Check

How do you add a new row to a SwiftUI list?

Recap: Adding Items

You can grow a list by appending to its @State array, often from a toolbar button or a TextField, with guards and animation. 🎉

よくある質問

「新しい項目の追加」レッスンは無料ですか?

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

「新しい項目の追加」で何を学びますか?

行を追加し、リストの状態を同期させます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「新しい項目の追加」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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